android三角形形状背景在imageview中不可见

时间:2016-02-26 08:20:22

标签: android background imageview shape

我想给应用程序中的imageview提供一个三角形背景。对于imageview,宽度为40dp,高度为23dp。但它没有显示背景。我将imageview的宽度和高度更改为匹配parent.Then背景是可见的。 下面是用于创建三角形的绘图。

trgl_shp.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <rotate
            android:fromDegrees="-45"
            android:pivotX="220%"
            android:pivotY="70%"
            android:toDegrees="45" >
            <shape android:shape="rectangle"
                >
                <stroke
                    android:width="0dp"
                    android:color="#00000000"
                    />

                <solid android:color="#00ACED" />
            </shape>
        </rotate>
    </item>
</layer-list>

pick_drop.xml:

<ImageView
            android:layout_width="40dp"
            android:layout_height="23dp"
            android:id="@+id/triangle1"
            android:background="@drawable/trgl_shp"
            android:layout_marginLeft="35dp"
            android:layout_below="@+id/pick"></ImageView>
        <ImageView
            android:layout_width="40dp"
            android:layout_height="23dp"
            android:id="@+id/triangle"
            android:background="@drawable/trgl_shp"
            android:layout_marginLeft="35dp"
            android:layout_below="@+id/drop"
            android:layout_alignLeft="@+id/drop"
            android:layout_alignStart="@+id/drop"
            android:layout_marginStart="28dp"></ImageView>

可绘制形状: enter image description here

1 个答案:

答案 0 :(得分:-1)

创建一个三角形形状Imageview .....

使用这个java文件.......

package com.customshape;

import android.widget.ImageView;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;





public class TriangleImageView extends ImageView {

    public TriangleImageView(Context ctx, AttributeSet attrs) {
        super(ctx, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        Drawable drawable = getDrawable();

        if (drawable == null) {
            return;
        }

        if (getWidth() == 0 || getHeight() == 0) {
            return;
        }
        Bitmap b = ((BitmapDrawable) drawable).getBitmap();
        Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);

        int w = getWidth(), h = getHeight();

        Bitmap roundBitmap = getRoundedCroppedBitmap(bitmap, w);
        canvas.drawBitmap(roundBitmap, 0, 0, null);

    }

    public static Bitmap getRoundedCroppedBitmap(Bitmap bitmap, int radius) {
        Bitmap finalBitmap;
        if (bitmap.getWidth() != radius || bitmap.getHeight() != radius)
            finalBitmap = Bitmap.createScaledBitmap(bitmap, radius, radius,
                    false);
        else
            finalBitmap = bitmap;
        Bitmap output = Bitmap.createBitmap(finalBitmap.getWidth(),
                finalBitmap.getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, finalBitmap.getWidth(),
                finalBitmap.getHeight());

        Point point1_draw = new Point(75, 0);
        Point point2_draw = new Point(0, 180);
        Point point3_draw = new Point(180, 180);

        Path path = new Path();
        path.moveTo(point1_draw.x, point1_draw.y);
        path.lineTo(point2_draw.x, point2_draw.y);
        path.lineTo(point3_draw.x, point3_draw.y);
        path.lineTo(point1_draw.x, point1_draw.y);
        path.close();
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(Color.parseColor("#BAB399"));
        canvas.drawPath(path, paint);
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(finalBitmap, rect, rect, paint);

        return output;
    }


}

并更改xml文件.....

<com.customshape.TriangleImageView
    android:id="@+id/imageView_triangle"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_marginTop="15dp"
    android:src="@drawable/ic_launcher" 
/>