- package com.nbadal.draw;
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.graphics.Rect;
- import android.view.View;
- public class DrawView extends View {
- private Paint paint;
- private BitmapFactory.Options options;
- private Bitmap bitmapOrg;
- private Bitmap target;
- private Bitmap bitmapRev;
- private Bitmap resizedBitmap;
- private int currY;
- public int drawX;
- public int drawY;
- public float drawSizeX;
- public float drawSizeY;
- private boolean drawGlasses;
- private boolean drawMirrored;
- public DrawView(Context context) {
- super(context);
- paint = new Paint();
- paint.setDither(false);
- paint.setAntiAlias(false);
- options = new BitmapFactory.Options();
- options.inDither = false;
- options.inScaled = false;
- bitmapOrg = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(),
- R.drawable.micro, options), 32, 5, false);
- bitmapRev = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(),
- R.drawable.reverse, options), 32, 5, false);
- drawGlasses = false;
- drawGlasses(150, 210, 570, false);
- }
- @Override
- protected void onDraw(Canvas canvas) {
- canvas.drawBitmap(target, 0, 0, paint);
- boolean moving = currY < drawY;
- if (moving) {
- currY++;
- }
- if (drawGlasses) {
- int newWidth = resizedBitmap.getWidth();
- int newHeight = resizedBitmap.getHeight();
- Paint bluey = new Paint();
- bluey.setColor(Color.argb(64, 0, 0, 255));
- canvas.drawRect(new Rect(drawX, currY, drawX + newWidth,
- currY + newHeight), bluey);
- canvas.drawBitmap(resizedBitmap, drawX, currY, paint);
- }
- if (moving) {
- invalidate();
- }
- }
- public void drawGlasses(int x1, int x2, int y, boolean mirror) {
- drawGlasses = true;
- drawMirrored = mirror;
- if (!mirror) {
- drawSizeX = (float) (x2 - x1) / (float) (25 - 16);
- drawSizeY = drawSizeX;
- drawY = y - (int)(1*drawSizeX);
- drawX = (int) (x1 - (drawSizeX * 16));
- } else {
- drawSizeX = (float) (x1 - x2) / (float) (25 - 16);
- drawSizeY = drawSizeX;
- drawY = y - (int)(1*drawSizeX);
- drawX = (int) (x1 - (drawSizeX * 16));
- }
- currY = -1;
- if (!drawMirrored) {
- resizedBitmap = Bitmap.createScaledBitmap(bitmapOrg,
- (int) (bitmapOrg.getWidth() * drawSizeX),
- (int) (bitmapOrg.getHeight() * drawSizeY), false);
- } else {
- resizedBitmap = Bitmap.createScaledBitmap(bitmapRev,
- (int) (bitmapRev.getWidth() * drawSizeX),
- (int) (bitmapRev.getHeight() * drawSizeY), false);
- }
- }
- public void setTargetPic(Bitmap targetPic) {
- target = targetPic;
- }
- }