如何使一半imageView透明?

时间:2018-05-29 09:05:11

标签: android bitmap android-imageview

我有一个_gl_old,我希望在不改变大小的情况下使其一半的位图透明 有人可以帮忙吗?

我想更改此图片:

enter image description here

到这一个:

enter image description here

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

3 个答案:

答案 0 :(得分: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);

答案 1 :(得分:0)

你可以试试这个:

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

答案 2 :(得分:0)

enter image description here

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

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

enter image description here

<?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>

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