【问题标题】:How to make half of an imageView transparent?如何使一半的 imageView 透明?
【发布时间】:2018-11-07 21:01:03
【问题描述】:

我有一个ImageView,我想让它的一半位图透明而不改变大小。
有人可以帮忙吗?

我想改变这张图片:

到这个:

但我不想改变高度和宽度。

【问题讨论】:

  • 发布您想要的图像设计。
  • 没错,我想在不改变分辨率的情况下将图像剪掉一半
  • @BhupatBheda 请检查已编辑的问题
  • 好的,您要显示 50% 的透明图像还是删除那部分?
  • @BhupatBheda 我想删除那部分。图片宽度很重要

标签: android bitmap android-imageview


【解决方案1】:

试试这个:

ImageView image= (ImageView) findViewById(R.id.image);
image.setAlpha(127); //value: [0-255]. Where 0 is fully transparent and 255 is fully opaque.

注意:

您可以在 setAlpha()

中使用浮点值

像这样:

image.setAlpha(0.5f);

【讨论】:

    【解决方案2】:

    <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/gd73i" 
            android:layout_marginLeft="-250dp"/>
    

    请检查您是否想要这样或尝试使用以下 xml 代码。

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/col_white">
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/gd73i" />
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                android:layout_centerInParent="true"
                >
                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:orientation="vertical"
                    android:layout_weight="1"
                    android:background="#fff">
    
                </LinearLayout>
    
                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:orientation="vertical"
                    android:layout_weight="1">
    
                   </LinearLayout>
    
            </LinearLayout>
            </RelativeLayout>
     </LinearLayout>
    

    你也可以像上面的代码那样使用空格。

    【讨论】:

      【解决方案3】:

      你可以试试这个:

      canvas.drawColor(Color.WHITE);   
      BitmapDrawable bd = getResources().getDrawable(R.drawable.load);    
      Bitmap bm = bd.getBitmap();    
      Paint paint = new Paint();    
      paint.setAlpha(60); 
      //provide transparent value here    
      canvas.drawBitmap(bm, 0, 0, paint);
      

      【讨论】:

      • 会影响所有图片
      • 您希望图像的一半是透明的,其余部分将保持正常。
      • 是的,这正是我想要的
      • 你可以看到这个问题,它可以帮助你。 stackoverflow.com/questions/21381135/…
      猜你喜欢
      • 1970-01-01
      • 2014-10-22
      • 1970-01-01
      • 2011-01-13
      • 1970-01-01
      • 1970-01-01
      • 2013-02-16
      • 2011-07-01
      • 1970-01-01
      相关资源
      最近更新 更多