快三个月了没写博客了,因为工作调动,很多经验、心得都没有时间记录下来。现在时间稍微充裕了点,我会尽量抽时间将之前想写而没写的东西补上。进入正题。
去年某个时候,我偶然看到一篇文章,讲android里面放大镜的实现。文章很乱,没有格式,基本上属于看不下去的那种。虽然体裁很有意思,但是我也没有足够的内力把它看完。不过看到一句关键的话,说是使用带圆形的Drawable。这句话就够了,他下面写的一堆东西我也懒得看,于是就自己开始尝试,然后就做出来了。现在代码贴出来分享。
Java代码
package chroya.demo.magnifier;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.view.MotionEvent;
import android.view.View;
/**
* 放大镜实现方式1
* @author chroya
*
*/
public class ShaderView extends View{
private Bitmap bitmap;
private ShapeDrawable drawable;
//放大镜的半径
private static final int RADIUS = 80;
//放大倍数
private static final int FACTOR = 3;
private Matrix matrix = new Matrix();
public ShaderView(Context context) {
super(context);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.show);
bitmap = bmp;
BitmapShader shader = new BitmapShader(
Bitmap.createScaledBitmap(bmp, bmp.getWidth()*FACTOR,
bmp.getHeight()*FACTOR, true), TileMode.CLAMP, TileMode.CLAMP);
//圆形的drawable
drawable = new ShapeDrawable(new OvalShape());
drawable.getPaint().setShader(shader);
drawable.setBounds(0, 0, RADIUS*2, RADIUS*2);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
final int x = (int) event.getX();
final int y = (int) event.getY();
//这个位置表示的是,画shader的起始位置










