- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我是 Android 的新手。我已经关注了这个问题并且我成功地缩放了图像这个代码工作正常有人可以帮助我如何编写代码以在单击按钮时放大缩小。我不知道如何完成这个任务
这是我遵循的教程 Tutorial Zoom Image并关注“Salman's Ayub Answer”
它工作正常,但我无法在放大和缩小图像时应用比例因子
最佳答案
您需要将这 2 个方法添加到您的 TouchImageView
类中:
public void zoomIn() {
oldScale = saveScale;
if(saveScale<=maxScale)
{
saveScale += .5;
matrix.setScale(saveScale, saveScale);
setImageMatrix(matrix);
invalidate();
// Center the image
// Center the image
if(bmHeight>bmWidth)
{
redundantXSpace = width - (saveScale * bmWidth);
redundantXSpace /= 2;
}
else
{
redundantYSpace = height - (saveScale * bmHeight) ;
redundantYSpace /= 2;
}
matrix.postTranslate(redundantXSpace , redundantYSpace );
setImageMatrix(matrix);
invalidate();
}
}
public void zoomOut() {
if(saveScale>=minScale)
{
saveScale -= .5;
matrix.setScale(saveScale, saveScale);
setImageMatrix(matrix);
invalidate();
// Center the image
if(bmHeight>bmWidth)
{
redundantXSpace = width - (saveScale * bmWidth);
redundantXSpace /= 2;
}
else
{
redundantYSpace = height - (saveScale * bmHeight) ;
redundantYSpace /= 2;
}
matrix.postTranslate(redundantXSpace , redundantYSpace );
setImageMatrix(matrix);
invalidate();
}
}
这是完整的 TouchImageView
类代码,以及这两个方法:
public class TouchImageView extends ImageView {
public Matrix matrix = new Matrix();
// We can be in one of these 3 states
static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
static final int CLICK = 3;
int mode = NONE;
float oldScale = 1.0f;
// Remember some things for zooming
PointF last = new PointF();
PointF start = new PointF();
float minScale = 1f;
float maxScale = 4f;
float[] m;
float redundantXSpace, redundantYSpace;
float width, height;
float saveScale = 1f;
float right, bottom, origWidth, origHeight, bmWidth, bmHeight;
ScaleGestureDetector mScaleDetector;
Context context;
public TouchImageView(Context context) {
super(context);
super.setClickable(true);
this.context = context;
mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
matrix.setTranslate(1f, 1f);
m = new float[9];
setImageMatrix(matrix);
setScaleType(ScaleType.MATRIX);
setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
mScaleDetector.onTouchEvent(event);
matrix.getValues(m);
float x = m[Matrix.MTRANS_X];
float y = m[Matrix.MTRANS_Y];
PointF curr = new PointF(event.getX(), event.getY());
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
last.set(event.getX(), event.getY());
start.set(last);
mode = DRAG;
break;
case MotionEvent.ACTION_POINTER_DOWN:
last.set(event.getX(), event.getY());
start.set(last);
mode = ZOOM;
break;
case MotionEvent.ACTION_MOVE:
if (mode == ZOOM || (mode == DRAG && saveScale > minScale)) {
Log.d("******", "ZOOM OR DRAG");
float deltaX = curr.x - last.x;
float deltaY = curr.y - last.y;
float scaleWidth = Math.round(origWidth * saveScale);
float scaleHeight = Math.round(origHeight * saveScale);
if (scaleWidth < width) {
deltaX = 0;
if (y + deltaY > 0)
deltaY = -y;
else if (y + deltaY < -bottom)
deltaY = -(y + bottom);
} else if (scaleHeight < height) {
deltaY = 0;
if (x + deltaX > 0)
deltaX = -x;
else if (x + deltaX < -right)
deltaX = -(x + right);
} else {
if (x + deltaX > 0)
deltaX = -x;
else if (x + deltaX < -right)
deltaX = -(x + right);
if (y + deltaY > 0)
deltaY = -y;
else if (y + deltaY < -bottom)
deltaY = -(y + bottom);
}
matrix.postTranslate(deltaX, deltaY);
last.set(curr.x, curr.y);
}else if(mode == DRAG && saveScale == minScale) {
Log.d("******", "DRAG");
}
break;
case MotionEvent.ACTION_UP:
mode = NONE;
int xDiff = (int) Math.abs(curr.x - start.x);
int yDiff = (int) Math.abs(curr.y - start.y);
if (xDiff < CLICK && yDiff < CLICK)
performClick();
break;
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
break;
}
setImageMatrix(matrix);
invalidate();
return true; // indicate event was handled
}
});
}
@Override
public void setImageBitmap(Bitmap bm) {
super.setImageBitmap(bm);
bmWidth = bm.getWidth();
bmHeight = bm.getHeight();
}
public void setMaxZoom(float x) {
maxScale = x;
}
private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
public boolean onScaleBegin(ScaleGestureDetector detector) {
mode = ZOOM;
return true;
}
@Override
public boolean onScale(ScaleGestureDetector detector) {
float mScaleFactor = detector.getScaleFactor();//(float)Math.min(Math.max(.95f, detector.getScaleFactor()), 1.05);
float origScale = saveScale;
saveScale *= mScaleFactor;
if (saveScale > maxScale) {
saveScale = maxScale;
mScaleFactor = maxScale / origScale;
} else if (saveScale < minScale) {
saveScale = minScale;
mScaleFactor = minScale / origScale;
}
right = width * saveScale - width - (2 * redundantXSpace * saveScale);
bottom = height * saveScale - height - (2 * redundantYSpace * saveScale);
if (origWidth * saveScale <= width || origHeight * saveScale <= height) {
matrix.postScale(mScaleFactor, mScaleFactor, width / 2, height / 2);
if (mScaleFactor < 1) {
matrix.getValues(m);
float x = m[Matrix.MTRANS_X];
float y = m[Matrix.MTRANS_Y];
if (mScaleFactor < 1) {
if (Math.round(origWidth * saveScale) < width) {
if (y < -bottom)
matrix.postTranslate(0, -(y + bottom));
else if (y > 0)
matrix.postTranslate(0, -y);
} else {
if (x < -right)
matrix.postTranslate(-(x + right), 0);
else if (x > 0)
matrix.postTranslate(-x, 0);
}
}
}
} else {
matrix.postScale(mScaleFactor, mScaleFactor, detector.getFocusX(), detector.getFocusY());
matrix.getValues(m);
float x = m[Matrix.MTRANS_X];
float y = m[Matrix.MTRANS_Y];
if (mScaleFactor < 1) {
if (x < -right)
matrix.postTranslate(-(x + right), 0);
else if (x > 0)
matrix.postTranslate(-x, 0);
if (y < -bottom)
matrix.postTranslate(0, -(y + bottom));
else if (y > 0)
matrix.postTranslate(0, -y);
}
}
return true;
}
}
@Override
protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width = MeasureSpec.getSize(widthMeasureSpec);
height = MeasureSpec.getSize(heightMeasureSpec);
//Fit to screen.
float scale;
float scaleX = width / bmWidth;
float scaleY = height / bmHeight;
scale = Math.min(scaleX, scaleY);
matrix.setScale(scale, scale);
setImageMatrix(matrix);
saveScale = 1f;
// Center the image
redundantYSpace = height - (scale * bmHeight) ;
redundantXSpace = width - (scale * bmWidth);
redundantYSpace /= 2;
redundantXSpace /= 2;
matrix.postTranslate(redundantXSpace, redundantYSpace);
origWidth = width - 2 * redundantXSpace;
origHeight = height - 2 * redundantYSpace;
right = width * saveScale - width - (2 * redundantXSpace * saveScale);
bottom = height * saveScale - height - (2 * redundantYSpace * saveScale);
setImageMatrix(matrix);
}
public TouchImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
super.setClickable(true);
this.context = context;
mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
matrix.setTranslate(1f, 1f);
m = new float[9];
setImageMatrix(matrix);
setScaleType(ScaleType.MATRIX);
setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
mScaleDetector.onTouchEvent(event);
matrix.getValues(m);
float x = m[Matrix.MTRANS_X];
float y = m[Matrix.MTRANS_Y];
PointF curr = new PointF(event.getX(), event.getY());
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
last.set(event.getX(), event.getY());
start.set(last);
mode = DRAG;
break;
case MotionEvent.ACTION_POINTER_DOWN:
last.set(event.getX(), event.getY());
start.set(last);
mode = ZOOM;
break;
case MotionEvent.ACTION_MOVE:
if (mode == ZOOM || (mode == DRAG && saveScale > minScale)) {
Log.d("******", "ZOOM OR DRAG");
float deltaX = curr.x - last.x;
float deltaY = curr.y - last.y;
float scaleWidth = Math.round(origWidth * saveScale);
float scaleHeight = Math.round(origHeight * saveScale);
if (scaleWidth < width) {
deltaX = 0;
if (y + deltaY > 0)
deltaY = -y;
else if (y + deltaY < -bottom)
deltaY = -(y + bottom);
} else if (scaleHeight < height) {
deltaY = 0;
if (x + deltaX > 0)
deltaX = -x;
else if (x + deltaX < -right)
deltaX = -(x + right);
} else {
if (x + deltaX > 0)
deltaX = -x;
else if (x + deltaX < -right)
deltaX = -(x + right);
if (y + deltaY > 0)
deltaY = -y;
else if (y + deltaY < -bottom)
deltaY = -(y + bottom);
}
matrix.postTranslate(deltaX, deltaY);
last.set(curr.x, curr.y);
}else if(mode == DRAG && saveScale == minScale) {
Log.d("******", "DRAG");
}
break;
case MotionEvent.ACTION_UP:
mode = NONE;
int xDiff = (int) Math.abs(curr.x - start.x);
int yDiff = (int) Math.abs(curr.y - start.y);
if (xDiff < CLICK && yDiff < CLICK)
performClick();
break;
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
break;
}
setImageMatrix(matrix);
invalidate();
return true; // indicate event was handled
}
});
}
public TouchImageView(Context context, AttributeSet attrs) {
super(context, attrs);
super.setClickable(true);
this.context = context;
mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
matrix.setTranslate(1f, 1f);
m = new float[9];
setImageMatrix(matrix);
setScaleType(ScaleType.MATRIX);
setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
mScaleDetector.onTouchEvent(event);
matrix.getValues(m);
float x = m[Matrix.MTRANS_X];
float y = m[Matrix.MTRANS_Y];
PointF curr = new PointF(event.getX(), event.getY());
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
last.set(event.getX(), event.getY());
start.set(last);
mode = DRAG;
break;
case MotionEvent.ACTION_POINTER_DOWN:
last.set(event.getX(), event.getY());
start.set(last);
mode = ZOOM;
break;
case MotionEvent.ACTION_MOVE:
if (mode == ZOOM || (mode == DRAG && saveScale > minScale)) {
Log.d("******", "ZOOM OR DRAG");
float deltaX = curr.x - last.x;
float deltaY = curr.y - last.y;
float scaleWidth = Math.round(origWidth * saveScale);
float scaleHeight = Math.round(origHeight * saveScale);
if (scaleWidth < width) {
deltaX = 0;
if (y + deltaY > 0)
deltaY = -y;
else if (y + deltaY < -bottom)
deltaY = -(y + bottom);
} else if (scaleHeight < height) {
deltaY = 0;
if (x + deltaX > 0)
deltaX = -x;
else if (x + deltaX < -right)
deltaX = -(x + right);
} else {
if (x + deltaX > 0)
deltaX = -x;
else if (x + deltaX < -right)
deltaX = -(x + right);
if (y + deltaY > 0)
deltaY = -y;
else if (y + deltaY < -bottom)
deltaY = -(y + bottom);
}
matrix.postTranslate(deltaX, deltaY);
last.set(curr.x, curr.y);
}else if(mode == DRAG && saveScale == minScale) {
Log.d("******", "DRAG");
}
break;
case MotionEvent.ACTION_UP:
mode = NONE;
int xDiff = (int) Math.abs(curr.x - start.x);
int yDiff = (int) Math.abs(curr.y - start.y);
if (xDiff < CLICK && yDiff < CLICK)
performClick();
break;
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
break;
}
setImageMatrix(matrix);
invalidate();
return true; // indicate event was handled
}
});
}
public void zoomIn() {
oldScale = saveScale;
if(saveScale<=maxScale)
{
saveScale += .5;
matrix.setScale(saveScale, saveScale);
setImageMatrix(matrix);
invalidate();
// Center the image
// Center the image
if(bmHeight>bmWidth)
{
redundantXSpace = width - (saveScale * bmWidth);
redundantXSpace /= 2;
}
else
{
redundantYSpace = height - (saveScale * bmHeight) ;
redundantYSpace /= 2;
}
matrix.postTranslate(redundantXSpace , redundantYSpace );
setImageMatrix(matrix);
invalidate();
}
}
public void zoomOut() {
if(saveScale>=minScale)
{
saveScale -= .5;
matrix.setScale(saveScale, saveScale);
setImageMatrix(matrix);
invalidate();
// Center the image
if(bmHeight>bmWidth)
{
redundantXSpace = width - (saveScale * bmWidth);
redundantXSpace /= 2;
}
else
{
redundantYSpace = height - (saveScale * bmHeight) ;
redundantYSpace /= 2;
}
matrix.postTranslate(redundantXSpace , redundantYSpace );
setImageMatrix(matrix);
invalidate();
}
}
}
这里是如何在放大/缩小按钮中使用它们的方法:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.LAYOUT_NAME);
Button zoonIn = (Button)findViewById(R.id.ZOOM_IN_BUTTON_ID);
Button zoonOut = (Button)findViewById(R.id.ZOOM_OUT_BUTTON_ID);
final TouchImageView touch = (TouchImageView)findViewById(R.id.YOUR_TOUCH_IMAGE_VIEW_)ID);
Bitmap bImage = BitmapFactory.decodeResource(this.getResources(), R.drawable.DRAWABLE_ID);
touch.setImageBitmap(bImage);
touch.setMaxZoom(4f); //change the max level of zoom, default is 3f
zoonIn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
touch.zoomIn();
}
});
zoonOut.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
touch.zoomOut();
}
});
}
希望对您有所帮助。
关于java - "Zoom In/out "图像在 Android 中单击按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21414345/
我在学习如何缩放方面经历了一段痛苦的时光。为了学习,我一直在尝试制作树状图缩放。我正在使用这个 jsfiddle 示例( http://jsfiddle.net/6kEpp/1/ )并尝试将其应用于没
使用 orbitcontrols.js(带有 THREE.js),我想在代码中实现与旋转鼠标滚轮相同的效果。例如,我想调用 camera.zoomIn() 之类的东西,让它向目标移动一段设定的距离。有
我已经创建了 map ,如 this link 所示并且运行良好。 但问题是,它只能以一种方式放大(只会变大)。我怎样才能让它以任何一种方式工作? 可能就像我们在 google map 上(加号 -
我正在寻找与 autocad ... 或 solidworks 类似的体验 基本上是内容大小(保持宽高比)Xsize * Ysize 并放大特定区域 并且我已经将 ruby/rails 作为我选择
在bootstrap的重置部分有一个 .clearfix { *zoom: 1; } 请问这里*zoom 和 zoom 有什么区别? 我很感激任何回答。 最佳答案 这是一个 css hack,这意味着
我在单独的子图中有 3 轴加速度计时间序列数据(t,x,y,z)的图,我想一起缩放。也就是说,当我在一个图上使用“缩放到矩形”工具时,当我释放鼠标时,所有 3 个图都会一起缩放。 以前,我只是使用不同
有没有一种简单的方法可以在页面刚加载时默认自动查看 Mapbox map 的所有标记...?这是我的 map :http://www.geometry.be/urbanmaestro/v7/非常感谢您
我已经使用 UIWebView 在 html 文件中设置了以下视口(viewport)标签。 现在,当用户选择时,我得到如图所示的效果。我想禁用此效果但仍然可以放大。 尝试将 UIWebView 的
我现在使用 Core-plot 进行 iPhone 图表开发。但它有一些我无法达到的要求。 我现在使用CorePlot 0.4,示例代码AAPLot来开发 请看下面的图片,然后你就会知道我的问题是什么
最新版本的 Google 地球附带了一项名为“缩放时自动倾斜”的功能。如果启用,Google Earth 会在您拉近表面时自动将相机倾斜到地平线。可以从 GUI 中禁用此功能(首选项 -> 导航选项卡
我找到了使用 rel="smallImage:image1.jpg" 等链接更改缩放图像的方法,但我是在运行时创建图像并将其添加到页面,所以页面首次加载时链接不会存在(这似乎需要存在)。当我在运行时生
我正在使用 ImageView 支持缩放。现在我正在扩展 ImageView 以便我可以在 canvas 上绘图。目前我有 setImageResource 设置一个 drawable 和在 onDr
在缩放行为上手动设置比例后,如何触发缩放事件? var zoom = d3.behavior.zoom() .scaleExtent([0.5, 4]) .on('zoom', onz
我有这个简单的测试代码: ul{ float:left; margin:0; list-style:none; paddi
我正在尝试实现一个具有以下外观的移动网站: 一个固定的标题 可滚动、可缩放的内容 首次加载页面时内容缩小 我一直在试验 IScroll 4,结果似乎不错,但有一个问题我找不到解决办法。我页面的内容是用
嗨,我是 iOS 初学者,在我的项目中,我以编程方式添加了一个 Collection View ,并且已成功添加。 我的要求是,当我单击 UICollectionViewCell 时,它将像下面的第二
遵循此处的建议:How to disable double click zoom for d3.behavior.zoom? 我在页面加载时禁用了双击缩放行为: https://bl.ocks.org
当用户使用鼠标滚轮滚入和滚出时,您可以调整缩放速度吗? 我的理解是 zoom.on (https://github.com/mbostock/d3/wiki/Zoom-Behavior#wiki-on
关于 css 样式和在浏览器中放大和缩小,我遇到了既有趣又奇怪的问题。 我创建了一个 Material ui 卡片,点击它时背景颜色会随着动画而改变。 动画效果很好,但如果您放大或缩小页面,浏览器会在
我将 JQTouch 用于 iPhone 应用程序。 JQtouch 默认情况下禁用捏合和缩放页面的可能性。对于一页(包含大图像),我需要启用捏合和缩放功能。这很容易: var viewport =
我是一名优秀的程序员,十分优秀!