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)

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

  1. imageview 自定义 android,Android自定义ImageView实现在图片上添加图层效果

    首先我们先看下效果图 实现思路 这是两张前后对比图,右边第二张图里面的已抢光标签图片当已经没有商品的时候就会显示了,在每个图片的中心位置,第一想法是在ImageView的外层再套一层RelativeL ...

  2. Android自定义动态布局 — 多图片上传

    Android自定义动态布局 - 多图片上传 本文介绍Android中动态布局添加图片,多图片上传. 项目中效果图:    技术点: 1.动态添加格局中的线条和添加图片的+号 2.多张图片异步上传 首 ...

  3. Android仿拼多多实现图片叠加部分覆盖效果

    Android仿拼多多实现图片叠加部分覆盖效果 需要实现的效果如下: 代码部分: AppIconStackView: public class AppIconStackView extends Vie ...

  4. 演练 鼠划图片上变亮的效果 1022

    演练 鼠划图片上变亮的效果 1022 期望效果 素材图片

  5. 如何在ps中添加图片上的塑料布效果

    1.塑料滤镜能够产生一种在照片表面蒙上一层塑料布的效果,接下来讲讲如何在ps中添加图片上的塑料布效果.打开PS,把素材导入PS中,Ctrl+J,复制一层图层,选择钢笔工具,将途中人物腰部一下的裙摆抠出 ...

  6. Android 使用GridView+仿微信图片上传功能(附源代码)

    由于工作要求最近在使用GridView完成图片的批量上传功能,我的例子当中包含仿微信图片上传.拍照.本地选择.相片裁剪等功能,如果有需要的朋友可以看一下,希望我的实际经验能对您有所帮助. 直接上图,下 ...

  7. Android简单实现将手机图片上传到服务器中

    在本例中,将会简单的实现安卓手机将图片上传到服务器中,本例使用到了 服务器端:PHP+APACHE 客户端:JAVA 先简单实现一下服务器端的上传并测试上传效果,看实例 <?php if(emp ...

  8. android之调用webservice实现图片上传

    http://www.cnblogs.com/top5/archive/2012/02/16/2354517.html 最近boss要求做android客户端的图片上传和下载,就是调用服务器的webs ...

  9. android dialog动画_2020年GitHub 上那些优秀Android开源库,这里是Top10!

    前言 每过一段时间呀,我都会给大家带来一些从Github上收集的一些开源库,有的是炫酷动效,有的则是实用的工具和类库.以前没看过或者没有收藏的同学,建议先收藏,以下是链接: [Android珍藏]推荐 ...

最新文章

  1. java des加密解密
  2. spring boot中使用Pagehelper实现分页
  3. 前瞻:Java能否畅行未来?
  4. go WaitGroup 简单示例
  5. 前端工程师和数据科学的快乐
  6. 【记事】今年的中秋节
  7. windows删文件:找不到该项目,该项目不在xx中,请确认位置,然后重试 的解决方案
  8. php mysqli result,PHP mysqli_free_result()与mysqli_fetch_array()函数详解
  9. 7.TCP/IP 详解卷1 --- Ping 程序
  10. 平均聚类系数_聚类方法排除CPU用量误报警
  11. 推荐几个Mac系统桌面吸色工具!颜色吸取器
  12. 关于安装office软件时和visio软件冲突
  13. 那些编程中遇到的常见英文缩写
  14. Vpro 相机操作类
  15. 2014中国民营企业500强在京津冀经济区、珠江三角洲、长江三角洲分布
  16. Java笔试面试题三(编程算法)
  17. 大华技术股份有限公司测开笔试题分享
  18. MySQL使用INTO OUTFILE和LOAD DATA INFILE导出导入百万级数据文件
  19. 音视频卡顿问题的原因及解决方案
  20. redis的incr和incrby命令

热门文章

  1. vs code打开新的文件后旧的文件被顶掉
  2. 安卓学习笔记37:利用OpenGL ES绘制平面图形
  3. Java Web学习笔记12:CKEditor在线编辑器
  4. Scala案例:词频统计
  5. 关于phi函数的积性性质的一个证明
  6. 2017.10.16 水管局长水管局长数据加强版 思考记录
  7. 【英语学习】【Daily English】U03 Leisure Time L03 Hiking and camping are some of his favorites.
  8. 基于yolov3和pythorch框架的火焰识别检测算法
  9. 使用idea 打jar包
  10. jedis使用pipline的方法