在我正在使用的應用程序中,在相對佈局中有2個獨立的圖像視圖。我想有兩個圖像的移動和轉動響應給定的動作事件的XML我的活動是:防止圖像視圖在觸摸和旋轉上重疊
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.vishalbisht.dragfeature.MainActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:id="@+id/Recycle"
android:layout_alignParentLeft="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Ring"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true" />
</RelativeLayout>
雖然該計劃的部分是:
public class MainActivity extends AppCompatActivity implements View.OnTouchListener {
ImageView jwl,rlo;
private final String TAG="DRAGFEATURE";
String Mode;
Bitmap bitmap,rotaBitmap;
BitmapDrawable bdr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
jwl= (ImageView) findViewById(R.id.Recycle);
rlo= (ImageView) findViewById(R.id.Ring);
jwl.setImageResource(R.drawable.icon);
rlo.setImageResource(R.drawable.ring);
jwl.setOnTouchListener(this);
rlo.setOnTouchListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
int eId = event.getActionMasked();
Mode = "Move";
switch (eId) {
case MotionEvent.ACTION_MOVE:
Log.v(TAG,"move image");
drag(v, event);
break;
case MotionEvent.ACTION_DOWN:
r = r + 13;
rotate(v);
break;
default:break;
}
if (v != jwl && v != rlo)
Log.v(TAG,"Not Valid");
return true;
}
private int r =2;
private void rotate(View v) {
if(v==jwl)
bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.icon);
else
bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.ring);
int w = bitmap.getWidth();
int h = bitmap.getHeight();
Matrix matrix = new Matrix();
matrix.preRotate(-r);
rotaBitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix,true);
bdr = new BitmapDrawable(rotaBitmap);
if(v==jwl)
jwl.setImageDrawable(bdr);
else
rlo.setImageDrawable(bdr);
}
private void drag(View v, MotionEvent event) {
RelativeLayout.LayoutParams mParams = (RelativeLayout.LayoutParams) jwl.getLayoutParams();
Log.v(TAG,"Start of Move");
int x = (int) event.getRawX();
int y = (int) event.getRawY();
mParams.leftMargin = x - 150;
mParams.topMargin = y - 210;
if(v==jwl)
jwl.setLayoutParams(mParams);
else
rlo.setLayoutParams(mParams);
}
}
當我觸摸或拖動左圖像(回收),那麼它的罰款,但當我點擊環,然後兩個圖像重疊和移動/旋轉在一起。爲什麼會發生這種情況,以及如何解決它?
我只是想你的解決方案,而這是移動ImageView的ID肯定更好屬性WRAP_CONTENT:回收當我觸摸Imageview ID時,我仍然遇到同樣的問題:響鈴或拖動它。 –