动态设置图像路径
在我的项目中,有许多图像具有后退和前进图像,并且所有图像具有相同的布局。在回退和下一个按钮时,应显示新图像。动态设置图像路径
private int imageCounter = 0;
private ImageView imageView;
private int[] imageArray = {R.drawable.image_w_lbl_0, R.drawable.image_w_lbl_1,};
private int index_count = 0;
@Override
public void onCreate(Bundle savedInstanceState)
{
setContentView(R.layout.frame0);//this one is the common parent layout for all imageviews
super.onCreate(savedInstanceState);
imageView = new ImageView(this);
ImageButton next = (ImageButton) findViewById(R.id.next);
ImageButton back = (ImageButton) findViewById(R.id.back);
next.setOnClickListener(this);
back.setOnClickListener(this);
//show the default image
this.loadImage(imageArray[0]);
}
@Override
public void onClick(View v)
{
int imagePath = 0;
// TODO Auto-generated method stub
switch (v.getId())
{
case R.id.next:
if(imageCounter < 25)
{
imagePath = imageArray[index_count];
}
imageCounter++;
break;
case R.id.back:
if(imageCounter > 0)
{
//imagePath =
}
imageCounter--;
break;
}
this.loadImage(imagePath);
}
private void loadImage(int imagePath)
{
imageView.setImageResource(imagePath);
}
我能看到我的布局,只有在有回输出,并与黑色的背景是从我绘制folder.Layout任何图像前进按钮:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
>
<ImageButton android:id="@+id/back" android:src="@drawable/btn_back"
的android:layout_width =“WRAP_CONTENT” 机器人:layout_height = “WRAP_CONTENT” 机器人:layout_gravity = “左” />
<ImageButton
机器人:ID =“@ + I d /下一个” 机器人:SRC = “@绘制/ btn_next” 机器人:layout_width = “WRAP_CONTENT” 机器人:layout_height = “WRAP_CONTENT” 机器人:layout_gravity = “右” />
</LinearLayout>
int数组:
Integer[] imageArray = { "R.drawable.img1", "R.drawable.img2" }
...
imageView.setImageResource(imageArray[0]); // displays img1
不非常清楚你在问什么,但你想要做这样的事情:
imagePath = R.drawable.image_wo_lbl_0;
如果你真的想要这样做,你可以用这种方式存储图像路径。从资源图像(即RES /绘制-MDPI)的
如何使此dynamin取决于计数值意味着为计数= 0获得特定图像的路径;为计数= 1获取另一个图像的路径......等等,只要将imagePath作为arguemnt方法loadimage并在imageview.that中显示该图像! – Alok 2010-11-10 13:42:24
嗨viv!谢谢我已经做了上述编辑的代码运行良好,但无法从我的可绘制文件夹中加载图像是有一些imageView.setImageResource(imagePath);;有一点我也很疑惑是,我的图像视图会重叠到我的下一个和后退按钮?看到我的xml请不要图像按钮!!我已采取上述布局作为父容器的所有图像加载 – Alok 2010-11-11 07:43:17
“我的图像视图重叠到我的下一个和后退按钮”,以及我不能判断它,对不起为它,为imageview尝试使用imageView.setBackgroundResource(imageArray [0])........希望它可以帮助 – viv 2010-11-11 07:53:39
imageView.setImageResource(R.drawable.img1)
使用ContextCompat
得到的绘制(图片你案件)。这是将图像设置到图像视图的新方法之一。
imageView.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(),
R.drawable.your_image));
您也可以使用ContextCompat
来获得颜色。 我在我的应用程序中使用它,它可以正常工作。
最简单的方法 - 可以考虑下面的代码
我们可以采取的ImageView setImageResource的优势,参考下列代码相同。在类似于下面的顺序
image_1.png,image_2.png提拉 放图像等
int imagePosition = 1;
int resId = getResources().getIdentifier("image_" + imagePosition, "drawable", getPackageName());
imageview.setImageResource(resId);
你想在你的Android应用程序或在您的构建环境得到这个信息。请提供更多细节。 – Robert 2010-11-10 11:13:56
在我的android应用程序 – Alok 2010-11-10 12:01:02