Advertisement
Guest User

MyView

a guest
Jun 13th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.34 KB | None | 0 0
  1. package com.example.karen.bitmap2;
  2.  
  3.  
  4.  
  5. import android.annotation.TargetApi;
  6. import android.content.Context;
  7. import android.graphics.Bitmap;
  8. import android.graphics.BitmapFactory;
  9. import android.graphics.Canvas;
  10. import android.util.AttributeSet;
  11. import android.view.View;
  12.  
  13. public class MyView extends View
  14. {
  15.     private Bitmap theBitmap1, theBitmap2;
  16.     private static final int WIDTH = 50;
  17.     private static final int HEIGHT = 50;
  18.     private static final int STRIDE = 64; //musi byc >= WIDTH
  19.  
  20.     public MyView(Context context) //konstruktory...
  21.     {
  22.         super(context);
  23.         initMyBitmaps();//wywołanie funkcji z dołu kodu
  24.     }
  25.  
  26.     public MyView(Context context, AttributeSet attrs) //AttributeSet - klasa która jest zbiorem własciwosci layoutu(wymiary,kolory itp)
  27.     {
  28.         super(context, attrs);
  29.         initMyBitmaps();
  30.     }
  31.  
  32.     public MyView(Context context, AttributeSet attrs, int defStyleAttr)
  33.     {
  34.         super(context, attrs, defStyleAttr);
  35.         initMyBitmaps();
  36.     }
  37.  
  38.     @TargetApi(21)
  39.     public MyView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
  40.     {
  41.         super(context, attrs, defStyleAttr, defStyleRes);
  42.         initMyBitmaps();
  43.     }
  44.  
  45.     private void initMyBitmaps()
  46.     {
  47.         Bitmap aBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.rocket);
  48.         final int dstWidth = 100;
  49.         final int dstHeight = 160;
  50.         theBitmap1 = Bitmap.createScaledBitmap(aBitmap, dstWidth, dstHeight, true);
  51.         int[] avColors = createColors();
  52.         theBitmap2 = Bitmap.createBitmap(avColors, 0, STRIDE, WIDTH, HEIGHT, Bitmap.Config.ARGB_8888);
  53.     }
  54.  
  55.     @Override
  56.     protected void onDraw(Canvas canvas)
  57.     {
  58.         canvas.drawBitmap(theBitmap1, 0, 0, null);
  59.         canvas.drawBitmap(theBitmap2, 150, 0, null);
  60.     }
  61.  
  62.     private static int[] createColors()
  63.     {
  64.         int[] colors = new int[STRIDE * HEIGHT];
  65.         for (int y = 0; y < HEIGHT; y++) {
  66.             for (int x = 0; x < WIDTH; x++) {
  67.                 int r = x * 255 / (WIDTH - 1);
  68.                 int g = y * 255 / (HEIGHT - 1);
  69.                 int b = 255 - Math.min(r, g);
  70.                 int a = Math.max(r, g);
  71.                 colors[y * STRIDE + x] = (a << 24) | (r << 16) | (g << 8) | b;
  72.             }
  73.         }
  74.         return colors;
  75.  
  76.  
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement