我正在将自定义视图加载到linearlayout中。自定义视图包含可以定位在线性布局内的图像。在图像定位后,如何确定哪些像素可见?
XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
android:id="@+id/viewPort"
android:orientation="horizontal"
android:layout_width="700dp"
android:layout_height="200dp">
</LinearLayout>
</RelativeLayout>
我的代码加载图片:
public TouchViewClass(Context context, AttributeSet attrs, int defStyle, String picPath) {
super(context, attrs, defStyle);
this.picPath = picPath;
//decode and size the image.
mSourceImage = prepareImage();
}
private Bitmap prepareImage(){
//Create bitmap options
final BitmapFactory.Options options = new BitmapFactory.Options();
//Create final bitmap with options specifying the new size
Bitmap readyImg = BitmapFactory.decodeFile(picPath, options);
return readyImg;
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save();
canvas.drawBitmap(mSourceImage, mPosX, mPosY, null);
canvas.restore();
}
和我加载视图的活动
View touchView = new TouchViewClass(this,mPicturePath);
LinearLayout rl = (LinearLayout)this.findViewById(R.id.viewPort);
rl.addView(touchView);
答案 0 :(得分:0)
好的想通了。如果其他人试图在被移动之后找到一个绘制的图像,那么我是如何做到的那样
@Override
public boolean onTouchEvent(MotionEvent ev) {
final int action = ev.getAction();
switch (action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN: {
final float x = ev.getX();
final float y = ev.getY();
mLastTouchX = x;
mLastTouchY = y;
// Save the ID of this pointer
mActivePointerId = ev.getPointerId(0);
break;
}
case MotionEvent.ACTION_MOVE: {
// Find the index of the active pointer and fetch its position
final int pointerIndex = ev.findPointerIndex(mActivePointerId);
//Get new location
final float x = ev.getX(pointerIndex);
final float y = ev.getY(pointerIndex);
//Compare to old location
final float dx = x - mLastTouchX;
final float dy = y - mLastTouchY;
//set new resting location
mPosX += dx;
mPosY += dy;
mLastTouchX = x;
mLastTouchY = y;
invalidate();
break;
}