Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android中的ImageView之间切换图像?_Android_Xml_Imageview - Fatal编程技术网

如何在android中的ImageView之间切换图像?

如何在android中的ImageView之间切换图像?,android,xml,imageview,Android,Xml,Imageview,我刚接触安卓,正在制作幻灯片拼图。我想通过在ImageView之间交换图像而不是滑动图像来保持简单。 我的代码没有给出任何错误,但也没有做任何事情。 你能帮帮我吗 这是我的xml代码: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="ma

我刚接触安卓,正在制作幻灯片拼图。我想通过在ImageView之间交换图像而不是滑动图像来保持简单。 我的代码没有给出任何错误,但也没有做任何事情。 你能帮帮我吗

这是我的xml代码:

<RelativeLayout 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" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="44dp"
        android:layout_marginTop="44dp"
        android:src="@drawable/ic_launcher"
        android:tag="1" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView1"
        android:layout_marginLeft="3dp"
        android:layout_toRightOf="@+id/imageView1"
        android:src="@drawable/ic_launcher" 
        android:tag="2" />

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView2"
        android:layout_marginLeft="3dp"
        android:layout_toRightOf="@+id/imageView2"
        android:src="@drawable/ic_launcher"
        android:tag="3"  />

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/imageView1"
        android:layout_below="@+id/imageView1"
        android:layout_marginTop="3dp"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/imageView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView4"
        android:layout_alignLeft="@+id/imageView2"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/imageView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView5"
        android:layout_alignLeft="@+id/imageView3"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/imageView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/imageView4"
        android:layout_below="@+id/imageView4"
        android:layout_marginTop="3dp"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/imageView8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView7"
        android:layout_alignLeft="@+id/imageView5"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/imageView9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView8"
        android:layout_alignRight="@+id/imageView6"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>
1) 不要经常使用findViewById。使用一次并保留链接。这是一个相当复杂的性能操作

2) 不要加载位图两次。您可以使用以下方法通过一次操作创建两倍缩放的位图

BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
BitmapFactory.decodeResource(getResources(), R.id.myimage, options);
(三)

将始终返回false,因为leeg是新创建的对象

不要像==那样比较对象内容。此运算符适用于比较基元类型。==运算符比较指向对象的链接,而不是对象的内容。您将只使用(object1==object2)检查它是否指向相同的对象实例,而不是具有相同数据的对象。要比较两个可绘图项(以及字符串等),请使用Object类中的equals()方法

if (image2.getDrawable().equals(leeg)) {

在调用imgView.getBackground并将背景设置为其他ImageView的源的click方法中,这是您想要的吗?你不应该叫getDrawable吗

我建议您使用ViewPager来实现此功能,而不是使用所有固定的代码。您还应该在onClick中对代码进行注释

更新: 尝试一下这个布局文件。我刚刚用水平滚动视图包装了你的,并修改了相对线的宽度

<HorizontalScrollView 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" >
<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="44dp"
        android:layout_marginTop="44dp"
        android:src="@drawable/ic_launcher"
        android:tag="1" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView1"
        android:layout_marginLeft="3dp"
        android:layout_toRightOf="@+id/imageView1"
        android:src="@drawable/ic_launcher" 
        android:tag="2" />

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView2"
        android:layout_marginLeft="3dp"
        android:layout_toRightOf="@+id/imageView2"
        android:src="@drawable/ic_launcher"
        android:tag="3"  />

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/imageView1"
        android:layout_below="@+id/imageView1"
        android:layout_marginTop="3dp"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/imageView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView4"
        android:layout_alignLeft="@+id/imageView2"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/imageView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView5"
        android:layout_alignLeft="@+id/imageView3"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/imageView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/imageView4"
        android:layout_below="@+id/imageView4"
        android:layout_marginTop="3dp"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/imageView8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView7"
        android:layout_alignLeft="@+id/imageView5"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/imageView9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView8"
        android:layout_alignRight="@+id/imageView6"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>

</HorizontalScrollView>


可能是浏览者?看不到ViewPager是否适合此处。getDrawable。。我会留意的。我对安卓不太了解,所以我正努力让它与我知道的东西一起工作谢谢你的帮助我不走我走的方向很好胡?你们中有谁碰巧对幻灯片拼图应该如何编程有一个简单的想法,因为我被卡住了。谢谢,我不知道我是怎么做到的,但我现在可以猜:)非常感谢你的帮助!!非常感谢!我已经做了更改,但当我单击它们时,图像仍然不会更改。你知道会是什么吗?提前谢谢
if (image2.getDrawable().equals(leeg)) {
<HorizontalScrollView 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" >
<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="44dp"
        android:layout_marginTop="44dp"
        android:src="@drawable/ic_launcher"
        android:tag="1" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView1"
        android:layout_marginLeft="3dp"
        android:layout_toRightOf="@+id/imageView1"
        android:src="@drawable/ic_launcher" 
        android:tag="2" />

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView2"
        android:layout_marginLeft="3dp"
        android:layout_toRightOf="@+id/imageView2"
        android:src="@drawable/ic_launcher"
        android:tag="3"  />

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/imageView1"
        android:layout_below="@+id/imageView1"
        android:layout_marginTop="3dp"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/imageView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView4"
        android:layout_alignLeft="@+id/imageView2"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/imageView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView5"
        android:layout_alignLeft="@+id/imageView3"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/imageView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/imageView4"
        android:layout_below="@+id/imageView4"
        android:layout_marginTop="3dp"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/imageView8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView7"
        android:layout_alignLeft="@+id/imageView5"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/imageView9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView8"
        android:layout_alignRight="@+id/imageView6"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>

</HorizontalScrollView>