12

ImageViewを使用して画像を設定しましたsetImageResource(R.drawable.icon1)

今私の要件は、に設定されている画像が何であるかを見つけて、ImageView何らかの処理を行うことです。

お気に入り

if (imageView.getImageResource() == R.drawable.icon1) {
  //do some processing
else if (imageView.getImageResource() == R.drawable.icon2) {
  //do somethign else
else 
 //display

したがって、メソッドが存在するかどうか(AFAIK、存在しない)、および に設定されているリソースを追跡する方法がない場合は知りたいですImageView

ありがとう。サナ。

4

5 に答える 5

7

整数を入れたので、整数を取り戻すことができると仮定していますが、それはうまくいきませんsetImageResource()ImageView#setImageResource()を参照してください。これは単なる便利な方法です。Android がバックグラウンドで行っていることは、Drawable リソース (ほとんどの場合、BitmapDrawable ですが、任意の型である可能性があります) を検索し、そのリソースを ImageView にBitmap オブジェクト (つまり、画像データのみ -- 元の「リソース ID」が以前に何であったかはわかりません)。

最善の解決策は、最後に使用したリソース ID を追跡することです。

imageView.setImageResource(R.drawable.image1);
this.mLastResourceId = R.drawable.image1;
// ...

// Later, when checking the resource:
if (this.mLastResourceId == R.drawable.image1) {
    // Do something
}
于 2011-06-15T02:37:27.067 に答える
0

別の代替手段は、可能であれば、ImageViewをサブクラス化し、オーバーライドされたsetImageResource()に整数を格納することです。

public class MyImageView extends ImageView  {
  int rememberId = -1;
  @override void setImageResource(int resId){
    rememberId = resId;
  }  
  int getMyResId(){
    return rememberId;
  }
}
于 2013-02-15T08:45:34.790 に答える
-1

Bundle を使用して、必要なプロパティを設定できるはずです。

Intent i = new Intent();      
Bundle extras = new Bundle();
      i.putExtra("prop", "value");
于 2011-06-15T02:11:29.763 に答える
-1
package com.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageButton;

/**
 * custom star type imagebutton that can be obtain the background image resource
 * id.
 * 
 * </p> your layout xml resource might be like the following code: <br>
 * <view class="com.widget.StarButton" <br>
 *      android:id="@+id/starButton" <br>
 *      android:layout_width="wrap_content" <br>
 *      android:layout_height="wrap_content" <br>
 *      android:background="@android:color/background_light" <br>
 *      android:paddingTop="10dp" <br>
 *      android:src="@drawable/star" /> <br>
 * 
 * @author Jeffen
 * 
 */
public class StarButton extends ImageButton {
private int mLastResourceId = -1;

public StarButton(Context context) {
    super(context);
}

public StarButton(Context context, AttributeSet attrs) {
    this(context, attrs, android.R.attr.imageButtonStyle);
}

public StarButton(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    setFocusable(true);
}

/**
 * set image resource and sync image resource id.
 */
@Override
public void setImageResource(int resId) {
    super.setImageResource(resId);

    setImageResourceId(resId);
}

public int getImageResourceId() {
    return mLastResourceId;
}

public void setImageResourceId(int resId) {
    mLastResourceId = resId;
}

}
于 2013-06-21T09:24:05.010 に答える