如何创建一个按钮以从Drawable生成随机图像 - Android Studio

时间:2017-08-14 20:46:50

标签: java android android-studio

我是android studio的新手,我的问题是: 我有8个可绘制的图像,名为ukchance1 ukchance2 ukchance3等。我的一个布局上有一个按钮和一个imageView。当我点击按钮时,我需要做些什么,随机的ukchance图像显示......

UkChanceLayout:

package com.austinthomas.monopolyactioncards.Activity;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher;

import com.austinthomas.monopolyactioncards.R;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;


public class UkChanceLayout extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.uk_chance);
}
}

uk_chance.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="UK - Chance"
    android:id="@+id/textView4"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true" />

<Button
    android:layout_width="300dp"
    android:layout_height="100dp"
    android:text="Click here for a Chance!"
    android:id="@+id/uk_chance_button"
    android:background = "#F44336"
    android:allowUndo="false"
    android:textColor="#FFFFFF"
    android:layout_marginBottom="32dp"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true" />

<ImageView
    android:layout_width="250dp"
    android:layout_height="143dp"
    android:id="@+id/uk_chance_image"
    android:layout_marginTop="37dp"
    android:layout_below="@+id/textView4"
    android:layout_centerHorizontal="true" />

</RelativeLayout>

修改 我尝试了larperdoodles方法,它说“不幸的是App已停止”

这是LogCat:

8-14 19:33:57.277 17380-17380/com.austinthomas.monopolyactioncards 
D/AndroidRuntime: Shutting down VM
08-14 19:33:57.278 17380-17380/com.austinthomas.monopolyactioncards 
E/AndroidRuntime: FATAL EXCEPTION: main

Process: com.austinthomas.monopolyactioncards, PID: 17380

java.lang.IllegalStateException: Could not find method clickHandler(View) in a 
parent or ancestor Context for android:onClick attribute defined on view class 
android.support.v7.widget.AppCompatButton with id 'uk_chance_button'

at 

android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327)

at 

android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)

at android.view.View.performClick(View.java:5198)

at android.view.View$PerformClick.run(View.java:21147)

at android.os.Handler.handleCallback(Handler.java:739)

at android.os.Handler.dispatchMessage(Handler.java:95)

at android.os.Looper.loop(Looper.java:148)

at android.app.ActivityThread.main(ActivityThread.java:5417)

at java.lang.reflect.Method.invoke(Native Method)

at 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
08-14 19:33:59.376 17380-17380/com.austinthomas.monopolyactioncards I/Process: 
Sending signal. PID: 17380 SIG: 9

2 个答案:

答案 0 :(得分:0)

使用此方法将根据您拥有的图像随机返回drawable。

public class UkChanceLayout extends AppCompatActivity {

    int min =1, max = 8;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.uk_chance);
      yourButton.setonclicklistener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
             myImage.setImageDrawable(getRandomDrawable(UkChanceLayout.this));
        }

    }

 public Drawable  getRandomDrawable(Context context){


    Random random = new Random();
    int i = random.nextInt(max - min + 1) + min;
    Drawable drawable = ContextCompat.getDrawable(context, defaultResID);;

    switch (i) {

        case 1:
            drawable = ContextCompat.getDrawable(context, resID1);
            break;
        case 2:
            drawable = ContextCompat.getDrawable(context, resID2);
            break;
        case 3:
            drawable = ContextCompat.getDrawable(context, resID3);
            break;
        case 4:
            drawable = ContextCompat.getDrawable(context, resID4);
            break;
        case 5:
            drawable = ContextCompat.getDrawable(context, resID5);
            break;
        case 6:
            drawable = ContextCompat.getDrawable(context, resID6);
            break;
        case 7:
            drawable = ContextCompat.getDrawable(context, resID7);
            break;
        case 8:
            drawable = ContextCompat.getDrawable(context, resID8);
            break;
    }

    return drawable;

   }
 } 

答案 1 :(得分:0)

将所有Drawable加载到常规java数组中。

int[] drawableIDs = {R.drawable.ukchance1, R.drawable.ukchance2... R.drawable.ukchance8};

然后在你的onCreate方法中,为你的uk_chance_button注册一个onClickListener。

 final Context context = this;
 final ImageView imageView = (ImageView) findViewById(R.id.uk_chance_image);
 Button buttonChance = (Button) findViewById(R.id.uk_chance_button);
     buttonChance.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
             //Randomly select an ID from the array
             int id = drawableIDs[new Random().nextInt(8)];
             Drawable drawable;
             //Check the build version to use the correct method of getting a drawable
             if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
                 drawable = context.getResources().getDrawable(id,getTheme());
             }else{
                 drawable = context.getResources().getDrawable(id);
             }
             //set the image view to display the drawable
             imageView.setImageDrawable(drawable);
         }
  });