首页 文章

动态设置图像路径

提问于
浏览
2

在我的项目中,有许多图像具有后向和前向图像,所有图像具有共同的布局 . 单击后退和下一个按钮时,应显示新图像 .

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);
}

我能够看到我的布局只在输出中有后退和前进按钮,黑色背景而不是我的drawable文件夹中的图像 .

XML布局:

<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"
        android:layout_height="wrap_content"
        android:layout_gravity="left"/>

    <ImageButton 
        android:id="@+id/next"
        android:src="@drawable/btn_next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right" />

</LinearLayout>

5 回答

  • 3

    使用 ContextCompat 获取drawable(在您的情况下为图像) . 这是将图像设置为图像视图的新方法之一 .

    imageView.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(),
                                                         R.drawable.your_image));
    

    您也可以使用 ContextCompat 来获取颜色 . 我在我的应用程序中使用它,它工作正常 .

  • 1

    来自资源的int数组(即res / drawable-mdpi):

    Integer[] imageArray = { "R.drawable.img1", "R.drawable.img2" }

    ...

    imageView.setImageResource(imageArray[0]); // displays img1

  • 1

    不是很清楚你在问什么,但你想做这样的事情:

    的ImagePath = R.drawable.image_wo_lbl_0;

    如果您确实想要这样做,可以以这种方式存储图像路径 .

  • 1
    imageView.setImageResource(R.drawable.img1)
    
  • -1

    最简单的方法 - 可以考虑以下代码

    我们可以利用Imageview setImageResource,参考下面的代码相同 . 将图像放在drawable中,如下面的顺序

    image_1.png,image_2.png等

    int imagePosition = 1;                    
    
    int resId = getResources().getIdentifier("image_" + imagePosition, "drawable", getPackageName());
                 imageview.setImageResource(resId);
    

相关问题