马克移动时图像拖动(平移和移动) [英] Mark is moving when image Drag(Pan & Move)
本文介绍了马克移动时图像拖动(平移和移动)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有使用搜索栏变焦自定义的ImageView。在ImageView的,我打标形象的一部分,但是当我移动的标记不与图像移动图像仍然不变。所以,请帮助我,如何使该商标在选定部分。
继code我已经习惯了放大:
公共类ImageZoomView扩展视图实现观测{ / **绘制位图时使用的涂料对象。 * /
私人最终油漆mPaint =新的油漆(Paint.FILTER_BITMAP_FLAG); / **矩形使用(重复使用),用于裁剪源图像。 * /
私人最终矩形mRectSrc =新的矩形(); / **矩形使用(和再使用)在画布上绘制指定区域。 * /
私人最终矩形mRectDst =新的矩形(); / **我们正在放大,并在屏幕上绘制位图。 * /
私人位图mBitmap;
私人位图mBitmap2; 私人ShapeDrawable之一;
私人ShapeDrawable 2;
/ ** pre-计算方面的商数。 * /
私人浮动mAspectQuotient; / **变焦的状态。 * /
私人ZoomState mState; //公共方法 / **
*构造
* /
公共ImageZoomView(上下文的背景下,ATTRS的AttributeSet){
超(背景下,ATTRS);
} / **
*设置位图图像
*
* @参数位图
*位图来查看和放大
* /
公共无效setImage(位图位图){
mBitmap =位图; calculateAspectQuotient(); 无效();
} / **
*设置位图图像
*
* @参数位图
*位图来查看和放大
* /
公共无效setImage2(位图bitmap2){
mBitmap2 = bitmap2; calculateAspectQuotient(); 无效();
} / **
*设置物体保持应使用的变焦状态
*
* @参数状态
*变焦状态
* /
公共无效setZoomState(ZoomState状态){
如果(mState!= NULL){
mState.deleteObserver(本);
} mState =状态;
mState.addObserver(本); 无效();
} //私有方法 私人无效calculateAspectQuotient(){
如果(mBitmap!= NULL){
mAspectQuotient =(((浮点)mBitmap.getWidth())/ mBitmap.getHeight())/(((浮动)的getWidth())/的getHeight());
Log.i(看点,+ mAspectQuotient);
}
} //超类覆盖 @覆盖
保护无效的onDraw(帆布油画){
如果(mBitmap = NULL&放大器;!&安培;!mState = NULL){
最终诠释viewWidth =的getWidth();
最终诠释viewHeight =的getHeight();
最终诠释bitmapWidth = mBitmap.getWidth();
最终诠释bitmapHeight = mBitmap.getHeight(); 最终浮动panX = mState.getPanX();
最终浮动PANY = mState.getPanY();
最终浮动zoomX = mState.getZoomX(mAspectQuotient * 0.5F)* viewWidth / bitmapWidth;
最终浮动zoomY = mState.getZoomY(mAspectQuotient * 0.5F)* viewHeight / bitmapHeight; Log.i(的onDraw,的onDraw+ SimpleSeekBarListener.f);
//设置源和目标矩形
mRectSrc.left =(int)的(panX * bitmapWidth - viewWidth /(zoomX * 2));
mRectSrc.top =(int)的(PANY * bitmapHeight - viewHeight /(zoomY * 2));
mRectSrc.right =(INT)(mRectSrc.left + viewWidth / zoomX);
mRectSrc.bottom =(INT)(mRectSrc.top + viewHeight / zoomY);
mRectDst.left = getLeft();
mRectDst.top =共达();
mRectDst.right = GetRight时();
mRectDst.bottom = getBottom(); //调整源矩形,以便它在源图像中适合。
如果(mRectSrc.left℃,){
mRectDst.left + = -mRectSrc.left * zoomX;
mRectSrc.left = 0;
}
如果(mRectSrc.right> bitmapWidth){
mRectDst.right - =(mRectSrc.right - bitmapWidth)* zoomX;
mRectSrc.right = bitmapWidth;
}
如果(mRectSrc.top℃,){
mRectDst.top + = -mRectSrc.top * zoomY;
mRectSrc.top = 0;
}
如果(mRectSrc.bottom> bitmapHeight){
mRectDst.bottom - =(mRectSrc.bottom - bitmapHeight)* zoomY;
mRectSrc.bottom = bitmapHeight;
}
// INT H1 = mBitmap.getHeight();
// Log.d(高度Befor,+ H1);
canvas.drawBitmap(mBitmap,mRectSrc,mRectDst,mPaint); }
} @覆盖
保护无效onLayout(布尔变化,诠释离开,诠释顶部,右诠释,诠释底部){
super.onLayout(改,左,上,右,下);
INT LEFT1 =左;
calculateAspectQuotient();
} //实现观察员
公共无效更新(可观察观察到,Object数据){
无效();
}}
解决方案
我觉得这是解决方案,您可以合并图像,以便试试这个方法:
私人位图annotateTheImage(位图thisBitmap){ 位图constructThisImage = Bitmap.createBitmap(thisBitmap.getWidth(),thisBitmap.getHeight(),Bitmap.Config.ARGB_8888);
帆布帆布=新的Canvas(constructThisImage); canvas.drawBitmap(thisBitmap,0,0,NULL); 对于(MyAnnotations点:annotationsPoints){
canvas.drawBitmap(point.getBitmap(),point.x,point.y,NULL);
} 返回constructThisImage;
}
和你通话的帆布这样的:
canvas.drawBitmap(annotateTheImage(mBitmap),mRectSrc,mRectDst,mPaint);
该mBitmap是在自定义的ImageView原始图像。
我希望这可以帮助您=)
I have a custom imageView with zoom using SeekBar. In that imageView I am Marking a portion of the image but when i move the image that mark is not moving with the image it remains same. So please help me , how to make that mark at the selected portion.
Following code I have Used To zoom:
public class ImageZoomView extends View implements Observer {
/** Paint object used when drawing bitmap. */
private final Paint mPaint = new Paint(Paint.FILTER_BITMAP_FLAG);
/** Rectangle used (and re-used) for cropping source image. */
private final Rect mRectSrc = new Rect();
/** Rectangle used (and re-used) for specifying drawing area on canvas. */
private final Rect mRectDst = new Rect();
/** The bitmap that we're zooming in, and drawing on the screen. */
private Bitmap mBitmap;
private Bitmap mBitmap2;
private ShapeDrawable one;
private ShapeDrawable two;
/** Pre-calculated aspect quotient. */
private float mAspectQuotient;
/** State of the zoom. */
private ZoomState mState;
// Public methods
/**
* Constructor
*/
public ImageZoomView(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* Set image bitmap
*
* @param bitmap
* The bitmap to view and zoom into
*/
public void setImage(Bitmap bitmap) {
mBitmap = bitmap;
calculateAspectQuotient();
invalidate();
}
/**
* Set image bitmap
*
* @param bitmap
* The bitmap to view and zoom into
*/
public void setImage2(Bitmap bitmap2) {
mBitmap2 = bitmap2;
calculateAspectQuotient();
invalidate();
}
/**
* Set object holding the zoom state that should be used
*
* @param state
* The zoom state
*/
public void setZoomState(ZoomState state) {
if (mState != null) {
mState.deleteObserver(this);
}
mState = state;
mState.addObserver(this);
invalidate();
}
// Private methods
private void calculateAspectQuotient() {
if (mBitmap != null) {
mAspectQuotient = (((float) mBitmap.getWidth()) / mBitmap.getHeight()) / (((float) getWidth()) / getHeight());
Log.i("Aspect",""+mAspectQuotient);
}
}
// Superclass overrides
@Override
protected void onDraw(Canvas canvas) {
if (mBitmap != null && mState != null) {
final int viewWidth = getWidth();
final int viewHeight = getHeight();
final int bitmapWidth = mBitmap.getWidth();
final int bitmapHeight = mBitmap.getHeight();
final float panX = mState.getPanX();
final float panY = mState.getPanY();
final float zoomX = mState.getZoomX(mAspectQuotient*0.5f) * viewWidth / bitmapWidth;
final float zoomY = mState.getZoomY(mAspectQuotient*0.5f) * viewHeight / bitmapHeight;
Log.i("onDraw", "onDraw" + SimpleSeekBarListener.f);
// Setup source and destination rectangles
mRectSrc.left = (int) (panX * bitmapWidth - viewWidth / (zoomX * 2));
mRectSrc.top = (int) (panY * bitmapHeight - viewHeight / (zoomY * 2));
mRectSrc.right = (int) (mRectSrc.left + viewWidth / zoomX);
mRectSrc.bottom = (int) (mRectSrc.top + viewHeight / zoomY);
mRectDst.left = getLeft();
mRectDst.top = getTop();
mRectDst.right = getRight();
mRectDst.bottom = getBottom();
// Adjust source rectangle so that it fits within the source image.
if (mRectSrc.left < 0) {
mRectDst.left += -mRectSrc.left * zoomX;
mRectSrc.left = 0;
}
if (mRectSrc.right > bitmapWidth) {
mRectDst.right -= (mRectSrc.right - bitmapWidth) * zoomX;
mRectSrc.right = bitmapWidth;
}
if (mRectSrc.top < 0) {
mRectDst.top += -mRectSrc.top * zoomY;
mRectSrc.top = 0;
}
if (mRectSrc.bottom > bitmapHeight) {
mRectDst.bottom -= (mRectSrc.bottom - bitmapHeight) * zoomY;
mRectSrc.bottom = bitmapHeight;
}
// int h1= mBitmap.getHeight();
// Log.d("Height Befor",""+h1);
canvas.drawBitmap(mBitmap, mRectSrc, mRectDst, mPaint);
}
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
int left1 = left;
calculateAspectQuotient();
}
// implements Observer
public void update(Observable observable, Object data) {
invalidate();
}
}
解决方案
I find this solution, you can merge images so try this method:
private Bitmap annotateTheImage(Bitmap thisBitmap) {
Bitmap constructThisImage = Bitmap.createBitmap(thisBitmap.getWidth(),thisBitmap.getHeight(),Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(constructThisImage);
canvas.drawBitmap(thisBitmap, 0, 0, null);
for (MyAnnotations point : annotationsPoints) {
canvas.drawBitmap(point.getBitmap(), point.x, point.y, null);
}
return constructThisImage;
}
and in you canvas call like this:
canvas.drawBitmap(annotateTheImage(mBitmap), mRectSrc, mRectDst, mPaint);
The mBitmap is the original image in your custom imageView.
I hope this help you =)
这篇关于马克移动时图像拖动(平移和移动)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文