Android按ID重新分类/更改图片onClick/更改imageView [英] Android Resouce by ID / Change image onClick / no change of imageView

查看:76
本文介绍了Android按ID重新分类/更改图片onClick/更改imageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,从数组(xml资源)中选择图像后,imageview不会改变,但会恢复为默认值.

I have a problem with my imageview not changing after picking an image from an array (xml resource), but falling back into the default value.

MeasurementActivity.java的内容

Content of my MeasurementActivity.java

public class MeasurementActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_measurement);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        final ImageView imageViewMeasurement = (ImageView) findViewById(measurement_image_view);

        //Button here to change the image in the imageView "imageViewMeasurement"
        Button button_like = (Button) findViewById(R.id.button_like);
        button_like.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Log.d("MYAPP", "Like-Button clicked");

                /*imageViewMeasurement.setImageResource(R.drawable.p13);*/

                TypedArray images = getResources().obtainTypedArray(R.array.images_primes);
                int chosenImageNumber = (int) (Math.random() * images.length());

                // setImageResource to the random chosenImageNumber
                imageViewMeasurement.setImageResource(images.getResourceId(chosenImageNumber, R.color.colorPrimaryDark));
                images.recycle();

                // Confirmation if the random generator picked a Number from the array
                String chosenImageNumberTest = String.valueOf(chosenImageNumber);
                Log.d("MYAPP Choice Number", chosenImageNumberTest);
            }

        });

    }    
}

我的array_images_measurement.xml的内容:

Content of my array_images_measurement.xml:

<resources>
    <array name="images_primes">
            <item>"@drawable/p1"</item>
            <item>"@drawable/p15"</item>
            <item>"@drawable/p20"</item>
            <item>"@drawable/p25"</item>
            <item>"@drawable/p30"</item>
            <item>"@drawable/p35"</item>
            <item>"@drawable/p40"</item>
            <item>"@drawable/p45"</item>
            <item>"@drawable/p50"</item>
            <item>"@drawable/p55"</item>
            <item>"@drawable/p60"</item>
            <item>"@drawable/p65"</item>
    </array>
    <array name="images_target">
            <item>"@drawable/t1"</item>
            <item>"@drawable/t5"</item>
            <item>"@drawable/t10"</item>
            <item>"@drawable/t15"</item>
            <item>"@drawable/t20"</item>
    </array>

activity_measurement.xml的内容:

Content of my activity_measurement.xml:

<RelativeLayout 
                android:layout_width="match_parent" 
                android:layout_height="match_parent" 
                android:layout_alignParentTop="true" 
                android:layout_alignParentStart="true">

        <ImageView 
                   android:id="@+id/measurement_image_view" 
                   android:layout_width="match_parent" 
                   android:layout_height="400dp" 
                   android:layout_alignParentLeft="true" 
                   android:text="Left" 
                   android:src="@drawable/starttest" 
                   android:padding="10dp" />
        <Button 
                android:text="@string/button_like" 
                android:layout_width="match_parent" 
                android:layout_height="wrap_content" 
                android:id="@+id/button_like" 
                android:layout_marginBottom="26dp" 
                android:layout_alignParentBottom="true" 
                android:layout_alignParentStart="true" /> 
</RelativeLayout>

预期行为:

  • 我单击按钮id:"button_like",图像"starttest"(在activity_measurement.xml中声明)变为从数组"images_primes"(11个可供选择的图像)中随机选择的图像.

实际情况:

  • 我单击按钮ID:"button_like",图像"starttest"将更改为默认颜色("R.color.colorPrimaryDark").
  • 日志输出似乎正常,每当我使用该按钮时,它就会显示另一个(随机)数字
  • 仅更改命令即可更改图像,而无需从xml资源中选择图像(imageViewMeasurement.setImageResource(R.drawable.p13));可以正常工作

问题:为什么图像不显示(或图像视图不刷新(?))?

Question: Why do the images not show up (or the imageview doesn't refresh (?))?

最佳 老虎码

推荐答案

您定义的数组错误.并且由于您的应用找不到正确的项目,因此它会返回到默认图片.可绘制对象是ID,ID是整数(您有字符串),请尝试以下操作:

You are defining your array wrong. and because your app can't find the correct item it goes back to the default image. a drawable is a id and an ID is an integer (you have strings) try this:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <array name="random_imgs">
        <item>@drawable/tests1</item>
        <item>@drawable/tests2</item>
        <item>@drawable/tests3</item>
    </array>
</resources>

用于获取可绘制对象

TypedArray imgs = getResources().obtainTypedArray(R.array.random_imgs);

Random r = new Random();
int i = random.nextInt(imgs.size() -1);

// get resource ID by index
imgs.getResourceId(i, R.color.colorPrimaryDark)

// or set you ImageView's resource to the id
mImgView1.setImageResource(imgs.getResourceId(i, R.color.colorPrimaryDark));

// recycle the array
imgs.recycle();

这篇关于Android按ID重新分类/更改图片onClick/更改imageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆