我正在尝试将图像从一个位置转换为另一个位置,同时用户拖动图像,图像正在移动,但它不准确移动,它只是跳跃,请帮助我,因为我是Android的新手。下面是我的XML文件,后面是java代码。谢谢..
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="230dp"
android:layout_marginTop="100dp"
android:layout_marginRight="400dp"
android:layout_marginBottom="100dp"
android:background="#000000"
android:src="@drawable/desert" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/imageView1"
android:layout_alignTop="@+id/imageView1"
android:layout_alignRight="@+id/imageView1"
android:layout_alignBottom="@+id/imageView1"
android:layout_margin="10dp"
android:layout_toRightOf="@+id/imageView1"
android:src="@drawable/logo" />
<ListView
android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="800dp" >
</ListView>
</RelativeLayout>
Java文件
public class Display extends ListActivity implements OnTouchListener
{
ArrayList<String> listimages;
ListView images;
ImageView imgback,imgfront;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.display);
images=(ListView) findViewById(android.R.id.list);
listimages=new ArrayList<String>();
listimages.add("/Pictures/Chrysanthemum.jpg");
listimages.add("/Pictures/Koala.jpg");
setListAdapter(new listAdapter(getApplicationContext(), listimages));
imgback=(ImageView) findViewById(R.id.imageView1);
imgfront=(ImageView) findViewById(R.id.imageView2);
//imgback.setImageBitmap("From Camera");
//Matrix mat=new Matrix();
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
Toast.makeText(getApplicationContext(), "Image: "+String.valueOf(position)+"
Clicked", Toast.LENGTH_LONG).show();
if(position==0)
{
imgfront.setImageBitmap(getbitmap("/sdcard/Pictures
/Koala.jpg"));
imgfront.setOnTouchListener(this);
}
else if(position==1)
{
imgfront.setImageBitmap(getbitmap("/sdcard/Pictures
/Chrysanthemum.jpg"));
}
}
public Bitmap getbitmap(String path)
{
Bitmap bitmap=BitmapFactory.decodeFile(path);
return bitmap;
}
@Override
public boolean onTouch(View arg0, MotionEvent arg1)
{
Toast.makeText(getApplicationContext(), "Touch Event Occured",
Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), "x value:
"+String.valueOf(arg1.getX())+" Y value: "+String.valueOf(arg1.getY()),
Toast.LENGTH_LONG).show();
imgfront.setTranslationX(arg1.getX());
imgfront.setTranslationY(arg1.getY());
return true;
}
}
答案 0 :(得分:2)
使用
imgfront.setTranslationX((arg1.getRawX() - imgfront.getWidth())/2);
imgfront.setTranslationY((arg1.getRawY() - imgfront.getHeight())/2);