Android放大镜的使用

package com.example.jinzhe.shapeviewdemo;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Path;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by jinzhe on 2016/8/2.
 */
public class ShaderView extends View{
    private Path mPath = new Path();
    private Matrix matrix = new Matrix();
    private Bitmap bitmap;
    //放大镜的半径
    private static final int RADIUS = 80;
    //放大倍数
    private static final int FACTOR = 2;
    private int mCurrentX, mCurrentY;


    public ShaderView(Context context) {
        super(context);
        mPath.addCircle(150, 150, RADIUS, Path.Direction.CW);
        matrix.setScale(FACTOR, FACTOR);

        bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.demo);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mCurrentX = (int) event.getX();
        mCurrentY = (int) event.getY();

        invalidate();
        return true;
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//底图
        canvas.drawBitmap(bitmap, 0, 0, null);
//剪切
        //改变画布的原点位置
        canvas.translate(mCurrentX - RADIUS, mCurrentY - RADIUS);
        canvas.clipPath(mPath);
//画放大后的图
        canvas.translate(RADIUS-mCurrentX*FACTOR, RADIUS-mCurrentY*FACTOR);
        canvas.drawBitmap(bitmap, matrix, null);
    }
}
 
在Activity中的设置
	
package com.example.jinzhe.shapeviewdemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.ViewGroup;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams
                .MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        ShaderView view = new ShaderView(getBaseContext());
        //是某一个构造函数的上下文
        addContentView(view, params);


    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值