1
我有一個問題:我不知道如何使我的layout
可縮放。我發現了一些技巧來解決這個問題的ImageView
或Bitmap
或Drawable
,或類似的東西,但不是layout
。我想放置一個自定義視圖的數組(實際上只是一個矩形,我忽略了onDraw
)。有沒有一些方法可以解決這個問題(我只是不想重複發明)或者我需要自己做所有事情?也許有人做過這樣的事情?我試圖用ScaleGestureDetector
做一些事情,但失敗了。我的代碼如下所示製作可縮放的自定義佈局
public class Seat extends View {
private float mLastTouchX;
private float mLastTouchY;
private float mPosX;
private float mPosY;
private float mWidth;
private float mHeight;
private static final int INVALID_POINTER_ID = -1;
public static final int ACTION_POINTER_INDEX_MASK = 0xff00;
public static final int ACTION_POINTER_INDEX_SHIFT = 8;
// The ‘active pointer’ is the one currently moving our object.
private int mActivePointerId = INVALID_POINTER_ID;
private ScaleGestureDetector mScaleDetector;
private float mScaleFactor = 1.f;
private boolean checked = false;
private boolean reserved = false;
private Place place;
public Seat(PlacesActivity context, Place place) {
super(context);
setOnClickListener(new OnSeatClickListener(context));
if (place.getReservation() != 1 || place.getStatus() != 1) {
reserved = true;
}
this.place = place;
this.mPosX = place.getX();
this.mPosY = place.getY();
mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((int) mWidth, (int) mHeight);
params.leftMargin = (int) mPosX;
params.topMargin = (int) mPosY;
setLayoutParams(params);
RelativeLayout parent = (RelativeLayout) getParent();
Place place = (Place) getTag();
for (int i = 0; i < parent.getChildCount(); i++) {
Seat seat = (Seat) parent.getChildAt(i);
Place placeToCompare = (Place) seat.getTag();
if (place.getId() == placeToCompare.getId()) {
parent.removeViewAt(i);
parent.addView(this, i, params);
}
}
if (reserved)
canvas.drawColor(Color.GRAY);
else if (checked)
canvas.drawColor(Color.RED);
else
canvas.drawColor(Color.GREEN);
}
public void setChecked() {
checked = true;
}
public void setUnchecked() {
checked = false;
}
public boolean isChecked() {
return checked;
}
public void setReserved() {
reserved = true;
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
// Let the ScaleGestureDetector inspect all events.
mScaleDetector.onTouchEvent(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;
mActivePointerId = ev.getPointerId(0);
break;
}
case MotionEvent.ACTION_MOVE: {
final int pointerIndex = ev.findPointerIndex(mActivePointerId);
final float x = ev.getX(pointerIndex);
final float y = ev.getY(pointerIndex);
// Only move if the ScaleGestureDetector isn't processing a gesture.
if (!mScaleDetector.isInProgress()) {
final float dx = x - mLastTouchX;
final float dy = y - mLastTouchY;
mPosX += dx;
mPosY += dy;
mWidth *= mScaleFactor;
mHeight *= mScaleFactor;
invalidate();
}
mLastTouchX = x;
mLastTouchY = y;
break;
}
case MotionEvent.ACTION_UP: {
mActivePointerId = INVALID_POINTER_ID;
break;
}
case MotionEvent.ACTION_CANCEL: {
mActivePointerId = INVALID_POINTER_ID;
break;
}
case MotionEvent.ACTION_POINTER_UP: {
final int pointerIndex = (ev.getAction() & ACTION_POINTER_INDEX_MASK)
>> ACTION_POINTER_INDEX_SHIFT;
final int pointerId = ev.getPointerId(pointerIndex);
if (pointerId == mActivePointerId) {
// This was our active pointer going up. Choose a new
// active pointer and adjust accordingly.
final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
mLastTouchX = ev.getX(newPointerIndex);
mLastTouchY = ev.getY(newPointerIndex);
mActivePointerId = ev.getPointerId(newPointerIndex);
}
break;
}
}
return true;
}
private class OnSeatClickListener implements OnClickListener {
private PlacesActivity context;
public OnSeatClickListener(PlacesActivity context) {
this.context = context;
}
@Override
public void onClick(View view) {
if (!reserved) {
Seat seat = (Seat) view;
seat.checked = !seat.checked;
view.invalidate();
Place place = (Place) view.getTag();
context.changeSeatState(place);
Toast toast = Toast.makeText(context, "Place " + place.getPlace() + " row " + place.getRow(), Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
}
}
private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureDetector detector) {
mScaleFactor *= detector.getScaleFactor();
// Don't let the object get too small or too large.
mScaleFactor = Math.max(0.5f, Math.min(mScaleFactor, 2.0f));
invalidate();
return true;
}
}
}
這是一類的觀點,我的佈局,我想放大含有一些這方面的意見。我試圖申請ScaleGestureDetector
,但也失敗了。
評論中onScroll方法的代碼來獲取變焦性能。由於需求,滾動被硬編碼。我正在尋找正確的滾動方式,讓我們看看... – user1169079
你是否找到了一些好方法 – pengwang