【问题标题】:The ImageView displays the image only if set to backgroundImageView 仅在设置为背景时才显示图像
【发布时间】:2019-08-27 21:30:33
【问题描述】:

我正在尝试将位图加载到ImageView
如果我这样做:

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.myimage);  
imageView.setImageBitmap(bitmap); 

什么都不显示。
如果我这样做:

        android:src="@drawable/myimage"

显示图像。
我的活动extends AppCompatActivity 和我的布局是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">



    <ImageView
        android:id="@+id/img"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp" />


</LinearLayout>

有什么想法吗?

【问题讨论】:

  • myimage 是什么类型的可绘制资源?
  • @smitty1:实际上我刚刚看到BitmapFactory.decodeResource(getResources(), R.drawable.myimage) 返回null。我什至尝试了android的drawable(例如ic_launcher_foreground)都不起作用(也返回null)
  • @smitty1:myimage 是矢量可绘制的
  • 如果尝试将矢量转换为位图,这将不起作用。你需要使用画布。
  • @Rajnishsuryavanshi:我该怎么做?

标签: android android-activity android-imageview android-image


【解决方案1】:

基本上,您不能将矢量图形直接转换为位图。您必须从可绘制对象创建位图,然后将其绘制到画布上

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

    Drawable drawable = getDrawable(R.drawable.ic_launcher_background);

    Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);

    imageView.setImageBitmap(bitmap);

【讨论】:

    【解决方案2】:

    试试这个代码

    Drawable drawable = ContextCompat.getDrawable(this, R.drawable.ic_android_black_24dp);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            drawable = (DrawableCompat.wrap(drawable)).mutate();
        }
    
        Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
                drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);
    
        imageView.setImageBitmap(bitmap);
    

    【讨论】:

      【解决方案3】:

      我你已经使用了矢量可绘制并尝试在 imageView 中以编程方式设置此可绘制。所以,使用此代码以编程方式设置矢量可绘制:

      第一道

       image_view=findViewById(R.id.image_view);
            image_view.setImageResource(R.drawable.ic_menu_send);
      

      第二种方式

      image_view=findViewById(R.id.image_view);
      
        Drawable drawable = getDrawable(R.drawable.ic_menu_send);
      
        Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);
        image_view.setImageBitmap(bitmap);
      

      【讨论】:

        【解决方案4】:

        确保您的可绘制文件不应是矢量文件。 如果 drawable 是矢量,则使用 app:srcCompat="@drawable/your_file" 否则你可以使用android:src="@drawable/your_file"

        并尝试像这样设置图像 image_view.setImageResource(R.drawable.ic_menu_send);

        【讨论】:

        • app:srcCompat 仅在使用 pre 21 api 时才需要。
        猜你喜欢
        • 1970-01-01
        • 2014-09-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-19
        相关资源
        最近更新 更多