获取可绘制图像 ID

Get a drawable image ID

我的可绘制文件夹中有很多图像。我想在点击按钮时将当前选择的图像设为墙纸。

int[] resources = {
    R.drawable.tssr_1,
    R.drawable.tssr_2,
    R.drawable.tssr_3,
    R.drawable.tssr_4,
    R.drawable.tssr_5,
    R.drawable.tssr_6
};

WallpaperManager myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());

try {
    //set wallpaper picture from resource here
    myWallpaperManager.setResource(R.drawable.tssr_1);
    //showToast("Success set as wallpaper");
} catch (IOException e) {
  //showToast("Error set as wallpaper");
}

这里 R.drawable.tssr_1 是硬编码的,因此应用程序成功地将 tssr_1 图像设置为墙纸图像。我想设置当前显示在屏幕上的图像。我如何获得该图像的 ID?

    ViewFlipper vf = new ViewFlipper(getApplicationContext());        
    ImageView image = new ImageView(getApplicationContext());
    // set resId as image's tag, then add to viewflipper.
    image.setTag(imageResId);
    vf.addView(image);

    // the listener will be invoked when image in flipper is clicked and held,
    // then you can set the image as your wall paper. 
    vf.setOnLongClickListener(new OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            int resId = (Integer) v.getTag(); // get image tag 
            myWallpaperManager.setResource(resId);
            return false;
        }
    });