天天看点

android 放大镜动画,Android在图片上进行放大镜效果(放大镜形状)

Android 图片上放大镜效果实现

1.[文件] ZoomView.java ~ 5KB     下载(55)

package com.study.hello;

import android.content.Context;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.graphics.Canvas;

import android.graphics.Paint;

import android.graphics.Path;

import android.graphics.Point;

import android.graphics.Rect;

import android.graphics.Bitmap.Config;

import android.graphics.Paint.Style;

import android.graphics.Path.Direction;

import android.graphics.drawable.BitmapDrawable;

import android.util.AttributeSet;

import android.view.Gravity;

import android.view.MotionEvent;

import android.view.View;

import android.widget.PopupWindow;

public class ZoomView extends View {

private static final int RADIUS = 58;

private static final int SIZE = 131;

private static final int HANDLE_SIZE = 33;

private static final long DELAY_TIME = 250;

private Rect srcRect;

private Point dstPoint;

private Bitmap magnifierBitmap;

private Bitmap handleBitmap;

private Bitmap maskBitmap;

private Bitmap resBitmap;

private Canvas canvas;

private Bitmap bg;

private PopupWindow popup;

private Magnifier magnifier;

public ZoomView(Context context, AttributeSet attrs) {

super(context, attrs);

// ╪стьм╪ф╛вйт╢

bg = BitmapFactory.decodeResource(getResources(), R.drawable.bg);

BitmapDrawable magnifierDrawable = (BitmapDrawable) context

.getResources().getDrawable(R.drawable.magnifier);

magnifierBitmap = magnifierDrawable.getBitmap();

BitmapDrawable handleDrawable = (BitmapDrawable) context.getResources()

.getDrawable(R.drawable.magnifier_handle);

handleBitmap = handleDrawable.getBitmap();

BitmapDrawable maskDrawable = (BitmapDrawable) context.getResources()

.getDrawable(R.drawable.mask);

maskBitmap = maskDrawable.getBitmap();

magnifier = new Magnifier(context);

popup = new PopupWindow(magnifier, SIZE, SIZE);

popup.setAnimationStyle(android.R.style.Animation_Toast);

srcRect = new Rect(0, 0, 2 * RADIUS, 2 * RADIUS);

dstPoint = new Point(0, 0);

}

@Override

public boolean onTouchEvent(MotionEvent event) {

int action = event.getAction();

if (action == MotionEvent.ACTION_DOWN

|| action == MotionEvent.ACTION_MOVE) {

int x = (int) event.getX();

int y = (int) event.getY();

srcRect.offsetTo(2 * x - RADIUS, 2 * y - RADIUS);

dstPoint.set(x - RADIUS, y - 3 * RADIUS / 2);

if (srcRect.left < 0) {

srcRect.offset(-srcRect.left, 0);

} else if (srcRect.right > resBitmap.getWidth()) {

srcRect.offset(resBitmap.getWidth() - srcRect.right, 0);

}

if (srcRect.top < 0) {

srcRect.offset(0, -srcRect.top);

} else if (srcRect.bottom > resBitmap.getHeight()) {

srcRect.offset(0, resBitmap.getHeight() - srcRect.bottom);

}

if (y < 0) {

// hide popup if out of bounds

popup.dismiss();

invalidate();

return true;

}

if (action == MotionEvent.ACTION_DOWN) {

removeCallbacks(showZoom);

postDelayed(showZoom, DELAY_TIME);

} else if (!popup.isShowing()) {

showZoom.run();

}

popup.update(getLeft() + dstPoint.x, getTop() + dstPoint.y, -1, -1);

magnifier.invalidate();

} else if (action == MotionEvent.ACTION_UP) {

removeCallbacks(showZoom);

// drawLayout();

popup.dismiss();

}

invalidate();

return true;

}

private void drawLayout() {

canvas.drawBitmap(bg, 0, 0, null);

}

Runnable showZoom = new Runnable() {

public void run() {

popup.showAtLocation(ZoomView.this, Gravity.NO_GRAVITY, getLeft()

+ dstPoint.x, getTop() + dstPoint.y);

}

};

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

int w = MeasureSpec.getSize(widthMeasureSpec);

int h = MeasureSpec.getSize(heightMeasureSpec);

createBitmap(2 * w, 2 * h);

super.onMeasure(widthMeasureSpec, heightMeasureSpec);

}

private void createBitmap(int w, int h) {

resBitmap = Bitmap.createBitmap(w, h, Config.ARGB_8888);

canvas = new Canvas(resBitmap);

drawLayout();

}

@Override

protected void onDraw(Canvas canvas) {

canvas.scale(0.5f, 0.5f);

canvas.drawBitmap(resBitmap, 0, 0, null);

canvas.drawBitmap(maskBitmap, 100, 50, null);

canvas.drawBitmap(maskBitmap, 100, 100, null);

}

class Magnifier extends View {

private Paint mPaint;

private Rect rect;

private Path clip;

public Magnifier(Context context) {

super(context);

mPaint = new Paint();

mPaint.setAntiAlias(true);

mPaint.setColor(0xff008000);

mPaint.setStyle(Style.STROKE);

rect = new Rect(0, 0, RADIUS * 2, RADIUS * 2);

clip = new Path();

clip.addCircle(2 + RADIUS, 2 + RADIUS, RADIUS, Direction.CW);

}

@Override

protected void onDraw(Canvas canvas) {

canvas.save();

canvas.clipPath(clip);

// draw popup

mPaint.setAlpha(255);

canvas.drawBitmap(resBitmap, srcRect, rect, mPaint);

canvas.restore();

// draw popup frame

mPaint.setAlpha(220);

canvas.drawBitmap(magnifierBitmap, 0, 0, mPaint);

// draw popup handle

mPaint.setAlpha(255);

canvas.drawBitmap(handleBitmap, SIZE - HANDLE_SIZE, SIZE

- HANDLE_SIZE, mPaint);

}

}

}

2.[文件] HelloWorld.java ~ 455B     下载(25)

package com.study.hello;

import android.app.Activity;

import android.os.Bundle;

public class HelloWorld extends Activity {

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

ZoomView zoomview = (ZoomView) findViewById(R.id.zoomview);

zoomview.bringToFront();

zoomview.setBackgroundColor(R.drawable.max);

}

}

3.[文件] color.xml ~ 114B     下载(25)

#ffffff

4.[文件] main.xml ~ 426B     下载(29)

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:id="@+id/mainLayout"

>

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_gravity="bottom"

android:id="@+id/zoomview"

/>

5.[文件] BitMapdemo.zip ~ 979KB     下载(654)