我的問題是我正在移動ImageView的圖像onTouchEvent,我在一個屏幕上有兩個圖像,但問題是如果我觸摸image2 &移動它是完美的工作,但如果我觸摸image1 image2移動到原來的位置,那麼問題是什麼?對不起,英語交流不好。圖像視圖的觸摸事件問題
請幫幫我。
以下是我的代碼: -
main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:layout_width="50sp" android:layout_height="50sp"
android:id="@+id/image" android:src="@drawable/image">
</ImageView>
<ImageView android:layout_y="30dip" android:layout_x="118dip"
android:layout_width="50sp" android:layout_height="50sp" android:id="@+id/image1"
android:src="@drawable/image1">
</ImageView>
</RelativeLayout>
MainActivity.java:-
public class MainActivity extends Activity implements OnTouchListener {
int windowwidth;
int windowheight;
private RelativeLayout.LayoutParams layoutParams;
private RelativeLayout.LayoutParams layoutParams1;
ImageView image, image1;
int x_cord, y_cord;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
windowwidth = getWindowManager().getDefaultDisplay().getWidth();
windowheight = getWindowManager().getDefaultDisplay().getHeight();
image = (ImageView) findViewById(R.id.image);
image.setOnTouchListener(this);
image1 = (ImageView) findViewById(R.id.image1);
image1.setOnTouchListener(this);
}
public boolean onTouch(View v, MotionEvent event) {
switch (v.getId()) {
case R.id.image:
System.out.println("Image is Touched");
image = (ImageView) findViewById(R.id.image);
layoutParams = (RelativeLayout.LayoutParams) image.getLayoutParams();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
int x_cord = (int) event.getRawX();
int y_cord = (int) event.getRawY();
if (x_cord > windowwidth) {
x_cord = windowwidth;
}
if (y_cord > windowheight) {
y_cord = windowheight;
}
layoutParams.leftMargin = x_cord - 25;
layoutParams.topMargin = y_cord - 75;
image.setLayoutParams(layoutParams);
break;
default:
break;
}
case R.id.image1:
System.out.println("Image 1 is Touched");
image1 = (ImageView) findViewById(R.id.image1);
layoutParams1 = (RelativeLayout.LayoutParams) image1
.getLayoutParams();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
x_cord = (int) event.getRawX();
y_cord = (int) event.getRawY();
if (x_cord > windowwidth) {
x_cord = windowwidth;
}
if (y_cord > windowheight) {
y_cord = windowheight;
}
layoutParams1.leftMargin = x_cord - 25;
layoutParams1.topMargin = y_cord - 75;
image1.setLayoutParams(layoutParams1);
break;
default:
break;
}
}
return true;
}
}
你的代碼與圖片瀏覽也(320 * 480如)工作,但問題是,如果我使用大分辨率圖像當時的圖像不動Properly.ifü有任何想法,然後告訴我。 –
Hello Raj如何在此代碼上執行放大,縮小和旋轉。請幫幫我。 –
這個小碼這是不可能看到此鏈接:http://dl.dropbox.com/u/38493970/Full.java利用這一點,可能的平移和縮放這個代碼集成到我們的代碼.. –