【问题标题】:Set the image dynamically in imageView in android在android的imageView中动态设置图片
【发布时间】:2015-09-19 11:28:47
【问题描述】:

我是 android 编程的新手。当显示时,我想在一个 xml 中动态设置图像。具体来说,我将我的项目相关图像存储在可绘制文件夹中。此外,我将图像名称存储在字符串变量中,并且动态地尝试将这些图像设置为 imageview。但是图片没有显示。

我的代码:

    protected void onCreate( Bundle savedInstanceState ) 
    {
       super.onCreate( savedInstanceState );
       setContentView( R.layout.stone );    

       imageView = ( ImageView )findViewById( R.id.stone_xxxx );

       Intent intent = getIntent();
       position = intent.getStringExtra( "POSITION" );

       if ( position == "0" )
       {
         int imageResource = getResources().getIdentifier(     "@drawable/stone_a1", null, getPackageName() );
        Drawable res = getResources().getDrawable(imageResource);
        imageView.setImageDrawable( res );
        }
    }

【问题讨论】:

  • 你能在 int imageResource = getResources().getIdentifier( "@drawable/stone_a1", null, getPackageName() ); 之后记录 imageResource 的值吗?

标签: android


【解决方案1】:

在 ImageView 中设置

iv.setImageResource(R.drawable.image)

【讨论】:

    【解决方案2】:

    试试这个

    final int sdk = android.os.Build.VERSION.SDK_INT;
    if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
        imageView.setBackgroundDrawable( getResources().getDrawable(R.drawable.stone_a1) );
    } else {
        imageView.setBackground( getResources().getDrawable(R.drawable.stone_a1));
    }
    

    如需更多帮助,请访问此处https://stackoverflow.com/a/12523109/5202007

    【讨论】:

    • @Ezazel,您是否尝试过调试它。我认为问题在于if ( position == "0" )
    • 是的,你是对的。它必须是: if ( position.equals( "0" ) ) 另外我只使用: imageView.setImageResource( R.drawable.stone_a1 )
    【解决方案3】:

    希望它对你有用..

    imagev1=(ImageView) findViewById(R.id.imageViewslider22);
                final int []imageArray={R.drawable.img1,R.drawable.img2,R.drawable.img3,R.drawable.img4,R.drawable.slider3,R.drawable.slider4,R.drawable.slider5,R.drawable.slider6,R.drawable.slider7};
    
                final Handler handler = new Handler();
                Runnable runnable = new Runnable() {
                int i=0;
                public void run() {
                            imagev1.setImageResource(imageArray[i]);
                                i++;
                                    if(i>imageArray.length-1)
                {
                i=0;    
                }
                handler.postDelayed(this, 30000);  //for interval...
                }
                };
                handler.postDelayed(runnable, 2000); //for initial delay..
    

    【讨论】:

      【解决方案4】:

      我发现了问题。

      必须是:if (position.equals("0")) 另外我只使用:imageView.setImageResource(R.drawable.stone_a1)

      【讨论】:

        【解决方案5】:
        ImageView imageView = new ImageView(this);
        Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.image);
        imageView.setImageBitmap(bmp);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-06-24
          • 1970-01-01
          • 2012-01-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多