自定义ImageView不显示图像

时间:2017-02-26 20:18:19

标签: java android imageview

我正在尝试使用底部的对角线切割实现一个imageview,我将用户在我的应用上显示图像。这是我采取的方法

  1. 我创建了一个扩展ImageView的自定义类。
  2. 然后我重写onDraw方法来绘制形状(这是我不确定我是否正在做正确的事情
  3.   

    我的自定义imageview类

    public class DiagonalSquare extends ImageView {
    
    private Context mContext;
    Paint paint ;
    Path path;
    
    public DiagonalSquare(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        setWillNotDraw(false);
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    }
    
    @Override
    protected void onDraw(Canvas canvas) {
    
        int w = getWidth(), h = getHeight();
        paint.setStrokeWidth(2);
        paint.setColor(Color.WHITE);
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        paint.setAntiAlias(true);
    
        path = new Path();
        path.setFillType(Path.FillType.EVEN_ODD);
    
        path.moveTo(w,h*3/4);
        path.lineTo(w,h);
        path.lineTo(0,h);
        path.close();
        canvas.drawPath(path, paint);
      }
    }
    

    The result I end up getting

    这是我最终得到的 - 蓝色部分是imageview。这是

    的xml
      <com.noel.CustomShape.DiagonalSquare
                android:id="@+id/background_image"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax"
                android:src="@drawable/bebe" />
    

2 个答案:

答案 0 :(得分:1)

您错过了对super.onDraw(canvas);

的来电

答案 1 :(得分:1)

除了@Dimezis回答:如果你想对ImageView背景产生透明效果,你必须清除硬件图层类型,并设置Xfermode疼痛,如下例所示:

package com.j2ko.customviews;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.widget.ImageView;

public class CustomImageView extends ImageView {
    private Context mContext;
    Paint paint ;
    Path path;

    public CustomImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        setWillNotDraw(false);
        //Move to XML if needed
        setBackgroundColor(Color.TRANSPARENT);
        setLayerType(LAYER_TYPE_HARDWARE, null);
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int w = getWidth(), h = getHeight();
        paint.setStrokeWidth(2);
        paint.setColor(Color.WHITE);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        paint.setAntiAlias(true);

        path = new Path();

        path.setFillType(Path.FillType.EVEN_ODD);

        path.moveTo(w,h*3/4);
        path.lineTo(w,h);
        path.lineTo(0,h);
        path.close();
        canvas.drawPath(path, paint);
    }
}

这将得到以下结果:

enter image description here