2013-03-15 46 views
1

我想將圖像從一個位置翻譯到另一個,而用戶拖動圖像,圖像正在移動,但它不移動準確,它只是跳躍,請幫助我,因爲我是新來的機器人。以下是我的XML文件,然後是java代碼。謝謝。在Android的平滑圖像whlie翻譯

<?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

試試這個'imgfront.setTranslationX(arg1.getRawX()); imgfront.setTranslationY(arg1.getRawY()); ' – 2013-03-15 11:01:17

+0

謝謝MoshErsan它的工作原理,但是當我碰到一個位置時它會向右下方移動,然後我需要拖動,你能幫助我嗎? – Ajeesh 2013-03-15 11:18:04

回答

2

使用

imgfront.setTranslationX((arg1.getRawX() - imgfront.getWidth())/2); 
imgfront.setTranslationY((arg1.getRawY() - imgfront.getHeight())/2);