1

我正在尝试在一个视图中使用多个TouchImageView。然后单击TouchImageView从图库中选择图像以显示在 TouchImageView 中。但问题是,当我在一个TouchImageView中设置来自画廊的新图像时,另一个也被重置意味着最后一次缩放仍然在该TouchImageView的右侧,但内部图像的最后一个位置被重新定位。

这是我的代码:

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" 
android:orientation="vertical">

<com.example.imagedragpinchtest.TouchImageView 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:id="@+id/TIV1"
    android:src="@drawable/ic_launcher"
    android:background="@drawable/dashline"
    android:layerType="software"
    />
<com.example.imagedragpinchtest.TouchImageView 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:id="@+id/TIV2"
    android:src="@drawable/ic_launcher"
    android:background="@drawable/dashline"
    android:layerType="software"/>
</LinearLayout>

MainActivity.java

package com.example.imagedragpinchtest;
public class MainActivity extends Activity {

TouchImageView tiv1,tiv2;
int selected = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tiv1 = (TouchImageView) findViewById(R.id.TIV1);
    tiv2 = (TouchImageView) findViewById(R.id.TIV2);
    tiv1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            selected = 1;
            Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(intent, selected);
        }
    });
    tiv2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            selected = 2;
            Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(intent, selected);
        }
    });

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode == RESULT_OK){
        String imgPath = getPathFromUri(data.getData());
        Log.e("new", imgPath);
        if(selected == 1)
            tiv1.setImageDrawable(new BitmapDrawable(getResources(), imgPath));
        if(selected == 2)
            tiv2.setImageDrawable(new BitmapDrawable(getResources(), imgPath));
    }

}

public String getPathFromUri(Uri uri) {
    String[] filePathColumn = { MediaStore.Images.Media.DATA };

    Cursor cursor = getContentResolver().query(uri,
            filePathColumn, null, null, null);
    cursor.moveToFirst();

    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
    String picturePath = cursor.getString(columnIndex);
    cursor.close();

    return picturePath;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

TouchImageView.java

我不知道问题出在哪里,我还尝试通过注释掉TouchImageView类的onSaveInstanceState()onRestoreInstanceState()来尝试。但这也解决不了问题。一些帮助将非常明显。提前致谢.....

4

0 回答 0