自定义ImageView放大镜控件

1.mMagnifierView.java 自定义控件类

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Path.Direction;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ImageView;

public class mMagnifierView extends ImageView {
	
	//放大镜的半径
	private static final int RADIUS = 80;
	//放大倍数
	private static final int FACTOR = 2;

	private Matrix matrix = new Matrix();
	
	
	public static final String TAG = "MagnifierView";

    private Paint mPaint = new Paint();
    private Bitmap mBitmap;
    private Bitmap mMagnifierBitmap;
    
    private float mPointX = -1.0f, mPointY = -1.0f;
    
    private boolean isFirstDraw = true;
    
    Handler mHandler = new Handler(){
            public void dispatchMessage(Message msg) {
                    switch(msg.what){
                    case -1:
                            postInvalidate();
                            break;
                    }
            };
    };

	public mMagnifierView(Context context, AttributeSet attrs) {
		super(context, attrs);
    	Bitmap bImage = ((BitmapDrawable)this.getDrawable()).getBitmap();
		
		mPaint.setAntiAlias(true);
      
	    mBitmap = bImage;
	      
	    matrix.setScale(FACTOR, FACTOR);
		
	}
	@Override
    protected void onDraw(Canvas canvas) {
            canvas.drawBitmap(mBitmap, 0, 0, mPaint);

            if (isFirstDraw) {
                    isFirstDraw = false;
            } else {
                    drawMagnifierPart(canvas);
            }
    }
	
    //被放大的图形
    private void drawMagnifierPart(Canvas canvas){
            if(mPointX == -1.0f || mPointY == -1.0f) return;
            
            Path path = new Path();
            path.addCircle(RADIUS, RADIUS, RADIUS, Direction.CW);
            
            //底图
    		canvas.drawBitmap(mBitmap, 0, 0, null);
    		//剪切,调整放大圆的位置
    		canvas.translate(mPointX - RADIUS, mPointY - RADIUS);
    		canvas.clipPath(path);	
    		//画放大后的图
    		canvas.translate(RADIUS-mPointX*FACTOR, RADIUS-mPointY*FACTOR);
    		
    		canvas.drawBitmap(mBitmap, matrix, null);
    		
    		
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
            switch(event.getAction()){
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
                    Log.d(TAG, "action move");
                    mPointX = event.getX();
                    mPointY = event.getY();
                    mHandler.sendEmptyMessage(-1);
                    break;
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                    mPointX = -1.0f;
                    mPointY = -1.0f;
                    mHandler.sendEmptyMessage(-1);
                    break;
            }
            return true;
    }
}

2.layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
	<TextView
		android:id="@+id/myTextView"
	    android:layout_width="fill_parent" 
	    android:layout_height="wrap_content" 
	    android:text="@string/hello"/>
	    
	<com.xu.service.mMagnifierView android:id="@+id/theImage"
		android:layout_height="fill_parent"
		android:layout_width="fill_parent"
		android:src="@drawable/show"></com.xu.service.mMagnifierView>
</LinearLayout>

按照需要自己调整下吧

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值