如何显示一个图像查看另一个图像?

时间:2014-11-08 12:00:30

标签: android imageview android-framelayout

我有一个带有2个图像视图的xml文件。第一个图像视图显示了一个填满屏幕的图像。当我触摸屏幕时,我希望第二个图像视图出现在该位置。我能实现这个吗?我使用的是frameLayout并尝试在ontouch事件期间设置第二个imageview的坐标,但它不断抛出错误。

我的XML文件

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >

<ImageView
    android:id="@+id/SingleView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scaleType="fitXY" />

<ImageView
    android:id="@+id/touch"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/lock"
    android:visibility="gone" />

</FrameLayout>

我的Java文件

 public class SingleViewActivity extends Activity {
int position;
String[] imageUrls;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.single_view);
    File Path = getExternalFilesDir(null);
    Constants cons = new Constants();
    imageUrls = cons.getImagePath(Path.toString());
    // Get intent data
    Intent i = getIntent();

    // Selected image id
    position = i.getExtras().getInt("position", -1);
    ImageView imageView = (ImageView) findViewById(R.id.SingleView);
    ImageLoader imageloader = ImageLoader.getInstance();
    imageloader.displayImage("file://" + imageUrls[position], imageView);
}

@SuppressWarnings("deprecation")
public boolean onTouchEvent(MotionEvent event) {
    // MotionEvent object holds X-Y values
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        final int XCood = (int) event.getX();
        final int YCood = (int) event.getY();
        ImageView touch = (ImageView) findViewById(R.id.touch);
        touch.setLayoutParams(new AbsoluteLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
                XCood, YCood));
        touch.setVisibility(View.VISIBLE);
    }
    return false;

}

}

logcat的

11-08 17:23:48.950: E/AndroidRuntime(1681): FATAL EXCEPTION: main
11-08 17:23:48.950: E/AndroidRuntime(1681): Process: com.p3authentication, PID: 1681
11-08 17:23:48.950: E/AndroidRuntime(1681): java.lang.ClassCastException: android.widget.AbsoluteLayout$LayoutParams cannot be cast to android.view.ViewGroup$MarginLayoutParams
11-08 17:23:48.950: E/AndroidRuntime(1681):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5116)
11-08 17:23:48.950: E/AndroidRuntime(1681):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
11-08 17:23:48.950: E/AndroidRuntime(1681):     at android.view.View.measure(View.java:16521)
11-08 17:23:48.950: E/AndroidRuntime(1681):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
11-08 17:23:48.950: E/AndroidRuntime(1681):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
11-08 17:23:48.950: E/AndroidRuntime(1681):     at android.view.View.measure(View.java:16521)
11-08 17:23:48.950: E/AndroidRuntime(1681):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
11-08 17:23:48.950: E/AndroidRuntime(1681):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
11-08 17:23:48.950: E/AndroidRuntime(1681):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
11-08 17:23:48.950: E/AndroidRuntime(1681):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
11-08 17:23:48.950: E/AndroidRuntime(1681):     at android.view.View.measure(View.java:16521)
11-08 17:23:48.950: E/AndroidRuntime(1681):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
11-08 17:23:48.950: E/AndroidRuntime(1681):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
11-08 17:23:48.950: E/AndroidRuntime(1681):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2552)
11-08 17:23:48.950: E/AndroidRuntime(1681):     at android.view.View.measure(View.java:16521)
11-08 17:23:48.950: E/AndroidRuntime(1681):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1915)
11-08 17:23:48.950: E/AndroidRuntime(1681):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1109)
11-08 17:23:48.950: E/AndroidRuntime(1681):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1291)
11-08 17:23:48.950: E/AndroidRuntime(1681):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)
11-08 17:23:48.950: E/AndroidRuntime(1681):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5603)
11-08 17:23:48.950: E/AndroidRuntime(1681):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
11-08 17:23:48.950: E/AndroidRuntime(1681):     at android.view.Choreographer.doCallbacks(Choreographer.java:574)
11-08 17:23:48.950: E/AndroidRuntime(1681):     at android.view.Choreographer.doFrame(Choreographer.java:544)
11-08 17:23:48.950: E/AndroidRuntime(1681):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
11-08 17:23:48.950: E/AndroidRuntime(1681):     at android.os.Handler.handleCallback(Handler.java:733)
11-08 17:23:48.950: E/AndroidRuntime(1681):     at android.os.Handler.dispatchMessage(Handler.java:95)
11-08 17:23:48.950: E/AndroidRuntime(1681):     at android.os.Looper.loop(Looper.java:136)
11-08 17:23:48.950: E/AndroidRuntime(1681):     at android.app.ActivityThread.main(ActivityThread.java:5146)
11-08 17:23:48.950: E/AndroidRuntime(1681):     at java.lang.reflect.Method.invokeNative(Native Method)
11-08 17:23:48.950: E/AndroidRuntime(1681):     at java.lang.reflect.Method.invoke(Method.java:515)
11-08 17:23:48.950: E/AndroidRuntime(1681):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
11-08 17:23:48.950: E/AndroidRuntime(1681):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
11-08 17:23:48.950: E/AndroidRuntime(1681):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

ImageView父布局是FrameLayout所以你必须初始化FrameLayout LayoutParams并在设置LayoutParams之后将X和Y坐标设置为LayoutParams左边和上边距到第二个ImageView,它在触摸位置显示第二个ImageView:

FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,FrameLayout.LayoutParams.WRAP_CONTENT);
params.leftMargin = (int) event.getX();
params.topMargin = (int) event.getY();
touch.setLayoutParams(params);

已编入代码:

public class SingleViewActivity extends Activity {

    private int position;
    private String[] imageUrls;
    private ImageView imageView;
    private ImageView touch;
    private  ImageLoader imageloader;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.single_view);
        imageView = (ImageView) findViewById(R.id.SingleView);
        touch = (ImageView) findViewById(R.id.touch);

        File Path = getExternalFilesDir(null);
        Constants cons = new Constants();
        imageUrls = cons.getImagePath(Path.toString());
        // Get intent data
        Intent i = getIntent();

        // Selected image id
        position = i.getExtras().getInt("position", -1);

        imageloader = ImageLoader.getInstance();
        imageloader.displayImage("file://" + imageUrls[position], imageView);

        imageView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,FrameLayout.LayoutParams.WRAP_CONTENT);
                    params.leftMargin = (int) event.getX();
                    params.topMargin = (int) event.getY();
                    touch.setLayoutParams(params);
                    touch.setVisibility(View.VISIBLE);
                }
                return false;
            }
        });
    }
}