当前位置:网站首页>ImageView supporting single finger sliding and double finger scaling
ImageView supporting single finger sliding and double finger scaling
2022-06-11 05:32:00 【Trace】
Support single finger sliding and double finger scaling ImageView
import android.content.Context;
import android.graphics.Point;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
/** * Use and normal imageview It doesn't make any difference , And this imageview It supports two finger scaling , Put it in any layout , The problem of gesture conflict such as nested sliding has been solved * Need to cooperate with MakeUpZoomImage Use * * @see MakeUpZoomImage */
public class ZoomImageView extends android.support.v7.widget.AppCompatImageView {
private static final String TAG = "ZoomImageView";
private double lastD;
private Point lastCenter;
private int pCont;
public ZoomImageView(Context context) {
super(context);
setClickable(true);
}
public ZoomImageView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
setClickable(true);
}
public ZoomImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setClickable(true);
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
pCont = 1;
return true;
case MotionEvent.ACTION_POINTER_DOWN:
pCont++;
if (pCont >= 2) {
setElevation(100);
bringToFront();
getParent().requestDisallowInterceptTouchEvent(true);
lastD = getDistance(event);
lastCenter = getCurrentPoint(event);
}
return true;
case MotionEvent.ACTION_POINTER_UP:
pCont--;
return true;
case MotionEvent.ACTION_MOVE:
if (event.getPointerCount() >= 2) {
double moved = getDistance(event) - lastD;
double viewSize = Math.sqrt(getWidth() * getWidth() + getHeight() * getHeight());
double scale = (moved / viewSize);
Log.d(TAG, scale + " " + lastD);
setVisibility(INVISIBLE);
Point point = getCurrentPoint(event);
float tx = point.x - lastCenter.x;
float ty = point.y - lastCenter.y;
MakeUpZoomImage.get().update(this, getDrawable(), scale + 1, tx, ty);
return true;
}
return true;
case MotionEvent.ACTION_UP:
MakeUpZoomImage.get().remove();
setVisibility(VISIBLE);
getParent().requestDisallowInterceptTouchEvent(false);
pCont = 0;
setScaleX(1);
setScaleY(1);
return true;
default:
break;
}
return super.onTouchEvent(event);
}
private Point getCurrentPoint(MotionEvent event) {
float x0 = event.getX(0);
float y0 = event.getY(0);
float x1 = event.getX(1);
float y1 = event.getY(1);
Point point = new Point(((int) (x1 + x0) / 2), ((int) (y1 + y0) / 2));
return point;
}
private double getDistance(MotionEvent event) {
float x0 = event.getX(0);
float y0 = event.getY(0);
float x1 = event.getX(1);
float y1 = event.getY(1);
return Math.sqrt((
(y1 - y0) * (y1 - y0))
+ ((x1 - x0) * (x1 - x0))
);
}
}
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
public class MakeUpZoomImage {
private Activity activity;
private volatile static MakeUpZoomImage makeUpZoomImage;
private ImageView imageView;
private MakeUpZoomImage(Activity activity) {
this.activity = activity;
imageView = new ImageView(activity);
activity.addContentView(imageView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
imageView.setVisibility(View.GONE);
}
public static MakeUpZoomImage attach(Activity activity) {
if (makeUpZoomImage == null) {
synchronized (MakeUpZoomImage.class) {
if (makeUpZoomImage == null) {
makeUpZoomImage = new MakeUpZoomImage(activity);
}
}
}
return makeUpZoomImage;
}
public static MakeUpZoomImage get() {
if (makeUpZoomImage == null) {
synchronized (MakeUpZoomImage.class) {
if (makeUpZoomImage == null) {
throw new IllegalStateException("Must be initialized");
}
}
}
return makeUpZoomImage;
}
public void release() {
remove();
makeUpZoomImage = null;
}
public void update(View view, Drawable drawable, double zoom, float tx, float ty) {
if (view == null) {
return;
}
int[] location = new int[2];
view.getLocationInWindow(location);
ViewGroup.LayoutParams lp = imageView.getLayoutParams();
lp.width = view.getWidth();
lp.height = view.getHeight();
imageView.setLayoutParams(lp);
imageView.setX(location[0] + tx);
imageView.setY(location[1] - getNotificationBarHeight() + ty);
imageView.setVisibility(View.VISIBLE);
imageView.setImageDrawable(drawable);
imageView.setScaleX((float) zoom);
imageView.setScaleY((float) zoom);
}
private int getNotificationBarHeight() {
Resources resources = activity.getResources();
int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
int height = resources.getDimensionPixelSize(resourceId);
return height;
}
public void remove() {
imageView.setVisibility(View.GONE);
imageView.setImageDrawable(null);
}
}
边栏推荐
- 在未来,机器人或 AI 还有多久才能具备人类的创造力?
- mysql字符串转数组,合并结果集,转成数组
- 49. grouping of acronyms
- Multithreading tutorial (XXVI) field updater and atomic accumulator
- BERT知识蒸馏
- 使用acme.sh自动申请免费SSL证书
- Games101 job 7-path tracing implementation process & detailed interpretation of code
- Deep search + backtracking
- Concurrent search set
- [go deep into kotlin] - get to know flow for the first time
猜你喜欢

【入门级基础】Node基础知识总结

Share 𞓜 jointly pre training transformers on unpaired images and text

22. Generate parentheses

袋鼠云数栈基于CBO在Spark SQL优化上的探索

WinForm (I) introduction to WinForm and use of basic controls

Reverse thinking: making cartoon photos real

Linked list de duplication

Zed2 running vins-mono preliminary test

Cascade EF gan: local focus progressive facial expression editing

Start the project using the locally configured gradle
随机推荐
Leetcode 161 Editing distance of 1 (2022.06.10)
[entry level basics] node basic knowledge summary
Wechat applet uploads the data obtained from database 1 to database 2
Multithreading tutorial (XXVIII) unsafe class
【深入kotlin】 - 初识 Flow
Zed2 camera manual
截取文件扩展名
Zed2 camera calibration -- binocular, IMU, joint calibration
Convert result set of SQL to set
AAAI2022-ShiftVIT: When Shift Operation Meets Vision Transformer
【项目篇- 附件佐证材料放什么?(十八种两千字总结)】创新创业竞赛项目计划书、挑战杯创业计划竞赛佐证材料
How to make lamps intelligent? How to choose single fire and zero fire intelligent switches!
Maximum number of points on the line ----- hash table solution
Paper recommendation: relicv2, can the new self supervised learning surpass supervised learning on RESNET?
推荐一款免费的内网穿透开源软件,可以在测试本地开发微信公众号使用
Multithreading tutorial (XXV) atomic array
es-ik 安装报错
Multithreading tutorial (XXI) double checked locking problem
PCB走线到底能承载多大电流
1.使用阿里云对象OSS(初级)