美文网首页
圆角视频

圆角视频

作者: couriravant | 来源:发表于2020-03-22 23:26 被阅读0次
package com.example.myapplication;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.*;
import android.support.annotation.ColorInt;
import android.util.AttributeSet;
import android.view.View;

public class CircularCoverView extends View {

private int leftTopRadians = 30; //leftTopRadians
 private int leftBottomRadians = 30; //leftBottomRadians
 private int rightTopRadians = 30; //rightTopRadians
 private int rightBottomRadians = 30; //rightBottomRadians

 private int coverColor = 0xffeaeaea; //color of cover.

 public CircularCoverView(Context context) {
this(context, null, 0);
 }

public CircularCoverView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
 }

public CircularCoverView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);

 TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CircularCoverView);
 leftTopRadians = typedArray.getDimensionPixelSize(R.styleable.CircularCoverView_left_top_radius, leftTopRadians);
 leftBottomRadians = typedArray.getDimensionPixelSize(R.styleable.CircularCoverView_left_bottom_radius, leftBottomRadians);
 rightTopRadians = typedArray.getDimensionPixelSize(R.styleable.CircularCoverView_right_top_radius, rightTopRadians);
 rightBottomRadians = typedArray.getDimensionPixelSize(R.styleable.CircularCoverView_right_bottom_radius, rightBottomRadians);
 coverColor = typedArray.getColor(R.styleable.CircularCoverView_cover_color, coverColor);
 }

/**
 * set radians of cover.
 */
 public void setRadians(int leftTopRadians, int rightTopRadians, int leftBottomRadians, int rightBottomRadians) {
this.leftTopRadians = leftTopRadians;
 this.rightTopRadians = rightTopRadians;
 this.leftBottomRadians = leftBottomRadians;
 this.rightBottomRadians = rightBottomRadians;
 }

/**
 * set color of cover.
 *
 * @param coverColor cover's color
 */
 public void setCoverColor(@ColorInt int coverColor) {
this.coverColor = coverColor;
 }

/**
 * create a sector-bitmap as the dst.
 *
 * @param w width of bitmap
 * @param h height of bitmap
 * @return bitmap
 */
 private Bitmap drawSector(int w, int h) {
Bitmap bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
 Canvas c = new Canvas(bm);
 Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
 p.setColor(0xFFFFCC44);//notice:cannot set transparent color here.otherwise cannot clip at final.

 c.drawArc(new RectF(0, 0, leftTopRadians * 2, leftTopRadians * 2), 180, 90, true, p);
 c.drawArc(new RectF(0, getHeight() - leftBottomRadians * 2, leftBottomRadians * 2, getHeight()), 90, 90, true, p);
 c.drawArc(new RectF(getWidth() - rightTopRadians * 2, 0, getWidth(), rightTopRadians * 2), 270, 90, true, p);
 c.drawArc(new RectF(getWidth() - rightBottomRadians * 2, getHeight() - rightBottomRadians * 2, getWidth(), getHeight()), 0, 90, true, p);
 return bm;
 }

/**
 * create a rect-bitmap as the src.
 *
 * @param w width of bitmap
 * @param h height of bitmap
 * @return bitmap
 */
 private Bitmap drawRect(int w, int h) {
Bitmap bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
 Canvas c = new Canvas(bm);
 Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
 p.setColor(coverColor);

 c.drawRect(new RectF(0, 0, leftTopRadians, leftTopRadians), p);
 c.drawRect(new RectF(0, getHeight() - leftBottomRadians, leftBottomRadians, getHeight()), p);
 c.drawRect(new RectF(getWidth() - rightTopRadians, 0, getWidth(), rightTopRadians), p);
 c.drawRect(new RectF(getWidth() - rightBottomRadians, getHeight() - rightBottomRadians, getWidth(), getHeight()), p);
 return bm;
 }

@Override
 protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
 Paint paint = new Paint();
 paint.setFilterBitmap(false);
 paint.setStyle(Paint.Style.FILL);

 //create a canvas layer to show the mix-result
 int sc = canvas.saveLayer(0, 0, getWidth(), getHeight(), null, Canvas.ALL_SAVE_FLAG );

 //draw sector-dst-bitmap at first.
 canvas.drawBitmap(drawSector(getWidth(), getHeight()), 0, 0, paint);
 //set Xfermode of paint.
 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));
 //then draw rect-src-bitmap
 canvas.drawBitmap(drawRect(getWidth(), getHeight()), 0, 0, paint);
 paint.setXfermode(null);
 //restore the canvas
 canvas.restoreToCount(sc);
 }
}

values/attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CircularCoverView">
<attr name="left_top_radius" format="dimension" />
<attr name="left_bottom_radius" format="dimension" />
<attr name="right_top_radius" format="dimension" />
<attr name="right_bottom_radius" format="dimension" />
<attr name="cover_color" format="color" />
</declare-styleable>
</resources>

方法二:

public class CorverView extends View {

    public CorverView(Context context) {
        super(context);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = TabListItemViewUtil.getPicWidth();
        int height = DensityUtil.dip2px(getContext(), 260);
        setMeasuredDimension(width, height);
    }

    public CorverView(Context context,  AttributeSet attrs) {
        super(context, attrs);
    }

    public CorverView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint();
        Path path = new Path();
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(getResources().getColor(R.color.colorGrayLight));
        paint.setStrokeWidth(5f);
        paint.setAntiAlias(true);
        path.setFillType(Path.FillType.INVERSE_EVEN_ODD);
        int mRadius = DensityUtil.dip2px(getContext(), 8);
        path.addRoundRect(new RectF(0, 0, getWidth(), getHeight()), new float[]{mRadius, mRadius, mRadius, mRadius, mRadius, mRadius, mRadius, mRadius}, Path.Direction.CW);
        canvas.drawPath(path, paint);
    }


}

相关文章

  • 圆角视频

    values/attrs.xml

  • 圆形视频和圆角视频的一种实现方式

    介绍 因为项目的需要需要实现圆角视频,一开始接到需求的时候是惊讶的,因为很少有圆角的视频(主要是一开始没有思路了。...

  • TextureView比SurfaceView好用的几个地方

    最近在做视频通话相关功能,发现有的效果不好实现。。 预览界面圆角效果 起初用传统的设置圆角的方式对Layout设置...

  • 关于Android 圆角 阴影

    1 圆角 阴影(支持所有View包括视频 GIF图等) 对于圆角,处理方式有很多种,但是好多都有所限制,比如不支持...

  • Flutter 部分圆角

    top 圆角 bottom 圆角 左边 圆角 右边 圆角

  • Image

    直接圆角图片 设置圆角图片度数 设置圆角图片带灰色圆角边框 设置圆角图片带灰色圆角边框带阴影

  • iOS开发-性能篇(持续更新中...)

    高效圆角 注意避免使用maskToBounds UIView的圆角 UILabel的圆角

  • 关于微信H5页面的图片和视频全屏问题

    项目需求 图片或者视频在页面中居中显示(边框圆角) 点击图片或者视频时,全屏显示/播放 看起来是很简单吧,但是移动...

  • iOS 绘制圆角

    级别: ★☆☆☆☆标签:「iOS切圆角」「layer圆角」「CAShapeLayer圆角」作者: Xs·H审校: ...

  • 圆角和边框

    圆角 圆角代码实现: 圆角User Defined Runtime Attributes实现: layer.cor...

网友评论

      本文标题:圆角视频

      本文链接:https://www.haomeiwen.com/subject/ggwbyhtx.html