android中的平滑图像翻译

android中的平滑图像翻译,android,Android,我试图在用户拖动图像时将图像从一个位置转换到另一个位置,图像在移动,但移动不准确,只是在跳跃,请帮助我,因为我是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" a

我试图在用户拖动图像时将图像从一个位置转换到另一个位置,图像在移动,但移动不准确,只是在跳跃,请帮助我,因为我是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;
}   

 }
公共类显示扩展了ListActivity实现的OnTouchListener
{
阵列列表图像;
列表视图图像;
ImageView imgback,imgfront;
@凌驾
创建时受保护的void(Bundle savedInstanceState)
{       
super.onCreate(savedInstanceState);
setContentView(R.layout.display);
images=(ListView)findviewbyd(android.R.id.list);
listimages=新的ArrayList();
添加(“/Pictures/jummy.jpg”);
添加(“/Pictures/Koala.jpg”);
setListAdapter(新的listAdapter(getApplicationContext(),listimages));
imgback=(ImageView)findViewById(R.id.imageView1);
imgfront=(ImageView)findViewById(R.id.imageView2);
//imgback.setImageBitmap(“来自摄影机”);
//矩阵矩阵=新矩阵();
}
@凌驾
受保护的void onListItemClick(列表视图l、视图v、整数位置、长id)
{       
super.onListItemClick(左、右、位置、id);
Toast.makeText(getApplicationContext(),“图像:+String.valueOf(位置)+”
单击“Toast.LENGTH_LONG).show();
如果(位置==0)
{           
setImageBitmap(getbitmap(“/sdcard/Pictures
/考拉(Koala.jpg),;
imgfront.setOnTouchListener(this);
}
否则如果(位置==1)
{
setImageBitmap(getbitmap(“/sdcard/Pictures
/菊花(jpg),;
}
}
公共位图getbitmap(字符串路径)
{
位图位图=BitmapFactory.decodeFile(路径);
返回位图;
}
@凌驾
公共布尔onTouch(视图arg0、运动事件arg1)
{
Toast.makeText(getApplicationContext(),“发生触摸事件”,
Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(),“x值:
“+String.valueOf(arg1.getX())+”Y值:“+String.valueOf(arg1.getY()),
Toast.LENGTH_LONG).show();
setTranslationX(arg1.getX());
setTranslationY(arg1.getY());
返回true;
}   
}
使用


试试这个
imgfront.setTranslationX(arg1.getRawX());setTranslationY(arg1.getRawY())谢谢你MoshErsan它工作了但当我触到一个位置时它会向右下移动,然后我需要拖动,你能帮我吗。。
imgfront.setTranslationX((arg1.getRawX() - imgfront.getWidth())/2); 
imgfront.setTranslationY((arg1.getRawY() - imgfront.getHeight())/2);