绘制位图在自定义的ImageView并获得坐标ImageView的不考虑设备 [英] Draw Bitmap in Custom ImageView and get the Co-ordinates of the ImageView irrespective of the device
问题描述
我想要得到的坐标的的ImageView
,无论设备的大小。
I want to get the co-ordinates of the ImageView
, Irrespective of device size.
有任何可能的方式!
我试图创建一个特定大小的ImageView
,即使对于父具体尺寸显示
也不过它不工作。
I have tried to create specific size for the ImageView
,Even specific size for the Parent View
also ,but its not working.
我曾尝试以下几种可能的方式。
I have tried the following possible ways.
int[] posXY = new int[2];
imageview.getLocationOnScreen(posXY);
int MoveX = posXY[0];
int MoveY = posXY[1];
我曾尝试与矩阵过,但没有工作。
I have tried with Matrix too ,But not working.
Matrix m = imageview.getImageMatrix();
试过低于code,但也不能正常工作。!!
Tried the below code, but it is also not working.!!
我需要得到同样的{X,Y}坐标为同一点(区域)的所有设备。
I need to get the same {x,y} Co-ordinates for all devices for the same point (Location).
final float[] getPointerCoords(ImageView view, MotionEvent e)
{
final int index = e.getActionIndex();
final float[] coords = new float[] {
e.getX(index), e.getY(index)
};
Matrix matrix = new Matrix();
view.getImageMatrix().invert(matrix);
matrix.mapPoints(coords);
return coords;
}
下面是绘制方法:
如果我设置的抽奖位图图像,它不适合屏幕的每一个设备。如果我设置图像显示的宽度和高度,我得到不同的坐标。
if i set bitmap image in draw, it does not fit the screen for every devices. If i set image with Display width and height, i am getting different co-ordinates.
@Override
public void draw(Canvas canvas) {
// TODO Auto-generated method stub
super.draw(canvas);
Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.subimage);
mBitmap = ((BitmapDrawable) drawable).getBitmap();
canvas.drawBitmap(mBitmap, 0, 0, null);
}
任何想法或帮助将是非常有益的。
Any Idea or help would be really helpful.
推荐答案
终于找到了一些相关的解决方案,同样的坐标为所有devices.Using活动,没有自定义ImageView的。
Finally found some relative solution,Same Co-ordinates for all devices.Using Activity,no Custom ImageView.
在ImageView的OnTouch ..
On ImageView OnTouch ..
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
final int index = e.getActionIndex();
final float[] coords = new float[] {
e.getX(index), e.getY(index)
};
Matrix matrix = new Matrix();
choosenImageView.getImageMatrix().invert(matrix);
matrix.mapPoints(coords);
return true;
}
使用绘图画布
Draw Using Canvas
Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.image);//drawable-nodpi
Bitmap bmp= ((BitmapDrawable) drawable).getBitmap();
alteredBitmap = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());
canvas = new Canvas(alteredBitmap);
paint = new Paint();
paint.setColor(Color.GREEN);
paint.setStrokeWidth(5);
matrix = new Matrix();
canvas.drawBitmap(bmp, matrix, paint);
这篇关于绘制位图在自定义的ImageView并获得坐标ImageView的不考虑设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!