Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.karen.bitmap2;
- import android.annotation.TargetApi;
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Canvas;
- import android.util.AttributeSet;
- import android.view.View;
- public class MyView extends View
- {
- private Bitmap theBitmap1, theBitmap2;
- private static final int WIDTH = 50;
- private static final int HEIGHT = 50;
- private static final int STRIDE = 64; //musi byc >= WIDTH
- public MyView(Context context) //konstruktory...
- {
- super(context);
- initMyBitmaps();//wywołanie funkcji z dołu kodu
- }
- public MyView(Context context, AttributeSet attrs) //AttributeSet - klasa która jest zbiorem własciwosci layoutu(wymiary,kolory itp)
- {
- super(context, attrs);
- initMyBitmaps();
- }
- public MyView(Context context, AttributeSet attrs, int defStyleAttr)
- {
- super(context, attrs, defStyleAttr);
- initMyBitmaps();
- }
- @TargetApi(21)
- public MyView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
- {
- super(context, attrs, defStyleAttr, defStyleRes);
- initMyBitmaps();
- }
- private void initMyBitmaps()
- {
- Bitmap aBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.rocket);
- final int dstWidth = 100;
- final int dstHeight = 160;
- theBitmap1 = Bitmap.createScaledBitmap(aBitmap, dstWidth, dstHeight, true);
- int[] avColors = createColors();
- theBitmap2 = Bitmap.createBitmap(avColors, 0, STRIDE, WIDTH, HEIGHT, Bitmap.Config.ARGB_8888);
- }
- @Override
- protected void onDraw(Canvas canvas)
- {
- canvas.drawBitmap(theBitmap1, 0, 0, null);
- canvas.drawBitmap(theBitmap2, 150, 0, null);
- }
- private static int[] createColors()
- {
- int[] colors = new int[STRIDE * HEIGHT];
- for (int y = 0; y < HEIGHT; y++) {
- for (int x = 0; x < WIDTH; x++) {
- int r = x * 255 / (WIDTH - 1);
- int g = y * 255 / (HEIGHT - 1);
- int b = 255 - Math.min(r, g);
- int a = Math.max(r, g);
- colors[y * STRIDE + x] = (a << 24) | (r << 16) | (g << 8) | b;
- }
- }
- return colors;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement