首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >流畅的图片在android中的翻译

流畅的图片在android中的翻译
EN

Stack Overflow用户
提问于 2013-03-15 18:55:55
回答 1查看 499关注 0票数 1

我正在尝试将图像从一个位置转换到另一个位置,而用户拖动该图像时,图像正在移动,但它移动得不准确,它只是在跳跃,请帮助我,因为我是android新手。下面是我的XML文件,后面是java代码。谢谢。。

代码语言:javascript
复制
<?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文件

代码语言:javascript
复制
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;
}   

 }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-15 22:38:04

使用

代码语言:javascript
复制
imgfront.setTranslationX((arg1.getRawX() - imgfront.getWidth())/2); 
imgfront.setTranslationY((arg1.getRawY() - imgfront.getHeight())/2);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15430568

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档