随笔 - 632  文章 - 17  评论 - 54  阅读 - 93万

Android放大镜效果的简单实现

复制代码
package com.example.myapi.pictobig;
import com.example.myapi.R;

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;  
import android.widget.ImageView;
  
/** 
 * 图片放大镜工具类 
 * @author yw-tony 
 * 
 */  
public class PicToBigTools extends ImageView{  
    private Bitmap bitmap;  
    private ShapeDrawable drawable;  
    /*放大镜的半径*/  
    private static final int RADIUS = 80;  
    /*放大倍数*/  
    private static final int FACTOR = 3;  
    private Matrix matrix = new Matrix();  
    /**
     * 构造方法
     * @param context
     */
    public PicToBigTools(Context context) {  
        super(context);  
        Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pictobig_icon);  
        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();  
        
        
        /**设置图片等比例缩放*/
        // 获得图片的宽高
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
         // 计算缩放比例
        float scaleWidth = ((float) RADIUS*2) / width;
        float scaleHeight = ((float) RADIUS*2) / height;
        matrix.postScale(scaleWidth,scaleHeight);
        
        
        //这个位置表示的是,画shader的起始位置  
        matrix.setTranslate(RADIUS-x*FACTOR, RADIUS-y*FACTOR);  
        drawable.getPaint().getShader().setLocalMatrix(matrix);  
        //bounds,就是那个圆的外切矩形  
        drawable.setBounds(x-RADIUS, y-RADIUS, x+RADIUS, y+RADIUS);  
        invalidate();//更新UI
        return true;  
    }  
      
    @Override  
    public void onDraw(Canvas canvas) {  
        super.onDraw(canvas);  
        canvas.drawBitmap(bitmap, 0, 0, null);  
        drawable.draw(canvas);//绘制图片  
    }  
}  
复制代码

 

posted on   飘杨......  阅读(2516)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示