Skip to content

Commit

Permalink
V1.5.2
Browse files Browse the repository at this point in the history
  • Loading branch information
GcsSloop committed Mar 21, 2018
1 parent 26ae9ca commit 7390401
Show file tree
Hide file tree
Showing 7 changed files with 373 additions and 145 deletions.
51 changes: 33 additions & 18 deletions RCLayout/app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
<?xml version="1.0" encoding="utf-8"?><!--
~ Copyright 2017 GcsSloop
~
~ Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -21,16 +20,15 @@
~ WebSite: http://www.gcssloop.com
-->

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#5F5652"
android:onClick="bgclick"
android:orientation="vertical"
android:padding="20dp"
android:onClick="bgclick"
tools:context="com.gcssloop.roundcornerlayouttest.MainActivity">

<com.gcssloop.widget.RCRelativeLayout
Expand All @@ -44,11 +42,11 @@
app:stroke_width="2dp">

<ImageView
android:onClick="click"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="click"
android:scaleType="centerCrop"
android:src="@drawable/test"/>
android:src="@drawable/test" />

</com.gcssloop.widget.RCRelativeLayout>

Expand All @@ -63,11 +61,11 @@
app:stroke_width="2dp">

<ImageView
android:onClick="click"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="click"
android:scaleType="centerCrop"
android:src="@drawable/test"/>
android:src="@drawable/test" />

</com.gcssloop.widget.RCRelativeLayout>

Expand All @@ -82,11 +80,11 @@
app:stroke_width="2dp">

<ImageView
android:onClick="click"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="click"
android:scaleType="centerCrop"
android:src="@drawable/test"/>
android:src="@drawable/test" />

</com.gcssloop.widget.RCRelativeLayout>

Expand All @@ -101,32 +99,49 @@
app:stroke_width="2dp">

<ImageView
android:onClick="click"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="click"
android:scaleType="centerCrop"
android:src="@drawable/test"/>
android:src="@drawable/test" />

</com.gcssloop.widget.RCRelativeLayout>

<com.gcssloop.widget.RCImageView
android:padding="10dp"
android:scaleType="centerCrop"
android:layout_centerHorizontal="true"
android:src="@drawable/test"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
app:round_corner="30dp"
app:round_corner_bottom_right="0dp"
app:round_corner_bottom_left="0dp"
app:clip_background="false"
android:background="#ccc"
app:stroke_color="#E39E83"
app:stroke_width="2dp" />

<com.gcssloop.widget.RCRelativeLayout
android:layout_width="240dp"
android:layout_height="300dp"
android:layout_centerInParent="true"
android:background="#fff"
android:padding="10dp"
app:clip_background="true"
app:round_as_circle="true"
app:round_corner="30dp"
android:padding="10dp"
android:background="#fff"
app:round_corner_bottom_right="0dp"
app:stroke_color="#66000000"
app:stroke_width="4dp">

<ImageView
android:onClick="click"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="click"
android:scaleType="centerCrop"
android:src="@drawable/test"/>
android:src="@drawable/test" />

<TextView
android:layout_width="match_parent"
Expand All @@ -136,7 +151,7 @@
android:gravity="center_horizontal"
android:text="害怕.png"
android:textColor="#fff"
android:textSize="24sp"/>
android:textSize="24sp" />

</com.gcssloop.widget.RCRelativeLayout>

Expand Down
2 changes: 1 addition & 1 deletion RCLayout/rclayout/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ dependencies {
ext {
GROUP_ID = "com.gcssloop.widget"
ARTIFACT_ID = "rclayout"
VERSION = "1.4.6"
VERSION = "1.5.2"
}

apply from: "/Applications/nexus/pack/pack-upload.gradle"
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright 2018 GcsSloop
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Last modified 2018-03-20 16:36:45
*
* GitHub: https://github.com/GcsSloop
* WeiBo: http://weibo.com/GcsSloop
* WebSite: http://www.gcssloop.com
*/

package com.gcssloop.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;

import com.gcssloop.widget.helper.RCHelper;

/**
* 作用:圆角图片
* 作者:GcsSloop
*/
@SuppressLint("AppCompatCustomView")
public class RCImageView extends ImageView {

RCHelper mRCHelper;

public RCImageView(Context context) {
this(context, null);
}

public RCImageView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}

public RCImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mRCHelper = new RCHelper();
mRCHelper.initAttrs(context, attrs);
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mRCHelper.onSizeChanged(this, w, h);
}

@Override
public void draw(Canvas canvas) {
if (mRCHelper.mClipBackground) {
canvas.save();
canvas.clipPath(mRCHelper.mClipPath);
super.draw(canvas);
canvas.restore();
} else {
super.draw(canvas);
}
}

@Override
protected void onDraw(Canvas canvas) {
canvas.saveLayer(mRCHelper.mLayer, null, Canvas.ALL_SAVE_FLAG);
super.onDraw(canvas);
mRCHelper.onClipDraw(canvas);
canvas.restore();
}

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (!mRCHelper.mAreaRegion.contains((int) ev.getX(), (int) ev.getY())) {
return false;
}
return super.dispatchTouchEvent(ev);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,37 +23,20 @@
package com.gcssloop.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.Region;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.RelativeLayout;

import com.gcssloop.rclayout.R;
import com.gcssloop.widget.helper.RCHelper;


/**
* 作用:圆角相对布局
* 作者:GcsSloop
*/
public class RCRelativeLayout extends RelativeLayout {
private float[] radii = new float[8]; // top-left, top-right, bottom-right, bottom-left
private Path mClipPath; // 剪裁区域路径
private Paint mPaint; // 画笔
private boolean mRoundAsCircle = false; // 圆形
private int mStrokeColor; // 描边颜色
private int mStrokeWidth; // 描边半径
private Region mAreaRegion; // 内容区域
private int mEdgeFix = 10; // 边缘修复
private RectF mLayer; // 画布图层大小
RCHelper mRCHelper;

public RCRelativeLayout(Context context) {
this(context, null);
Expand All @@ -65,97 +48,40 @@ public RCRelativeLayout(Context context, AttributeSet attrs) {

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

TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.RCRelativeLayout);
mRoundAsCircle = ta.getBoolean(R.styleable.RCRelativeLayout_round_as_circle, false);
mStrokeColor = ta.getColor(R.styleable.RCRelativeLayout_stroke_color, Color.WHITE);
mStrokeWidth = ta.getDimensionPixelSize(R.styleable.RCRelativeLayout_stroke_width, 0);
int roundCorner = ta.getDimensionPixelSize(R.styleable.RCRelativeLayout_round_corner, 0);
int roundCornerTopLeft = ta.getDimensionPixelSize(
R.styleable.RCRelativeLayout_round_corner_top_left, roundCorner);
int roundCornerTopRight = ta.getDimensionPixelSize(
R.styleable.RCRelativeLayout_round_corner_top_right, roundCorner);
int roundCornerBottomLeft = ta.getDimensionPixelSize(
R.styleable.RCRelativeLayout_round_corner_bottom_left, roundCorner);
int roundCornerBottomRight = ta.getDimensionPixelSize(
R.styleable.RCRelativeLayout_round_corner_bottom_right, roundCorner);
ta.recycle();

radii[0] = roundCornerTopLeft;
radii[1] = roundCornerTopLeft;

radii[2] = roundCornerTopRight;
radii[3] = roundCornerTopRight;

radii[4] = roundCornerBottomRight;
radii[5] = roundCornerBottomRight;

radii[6] = roundCornerBottomLeft;
radii[7] = roundCornerBottomLeft;

mClipPath = new Path();
mAreaRegion = new Region();
mPaint = new Paint();
mPaint.setColor(Color.WHITE);
mPaint.setAntiAlias(true);
mRCHelper = new RCHelper();
mRCHelper.initAttrs(context, attrs);
}

@Override protected void onSizeChanged(int w, int h, int oldw, int oldh) {
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mLayer = new RectF(0, 0, w, h);
RectF areas = new RectF();
areas.left = getPaddingLeft();
areas.top = getPaddingTop();
areas.right = w - getPaddingRight();
areas.bottom = h - getPaddingBottom();
mClipPath.reset();
if (mRoundAsCircle) {
float d = areas.width() >= areas.height() ? areas.height() : areas.width();
float r = d / 2;
PointF center = new PointF(w / 2, h / 2);
mClipPath.addCircle(center.x, center.y, r, Path.Direction.CW);
mClipPath.moveTo(-mEdgeFix, -mEdgeFix); // 通过空操作让Path区域占满画布
mClipPath.moveTo(w + mEdgeFix, h + mEdgeFix);
} else {
mClipPath.addRoundRect(areas, radii, Path.Direction.CW);
}
Region clip = new Region((int) areas.left, (int) areas.top,
(int) areas.right, (int) areas.bottom);
mAreaRegion.setPath(mClipPath, clip);
mRCHelper.onSizeChanged(this, w, h);
}

@Override protected void dispatchDraw(Canvas canvas) {
canvas.saveLayer(mLayer, null, Canvas.ALL_SAVE_FLAG);

@Override
protected void dispatchDraw(Canvas canvas) {
canvas.saveLayer(mRCHelper.mLayer, null, Canvas.ALL_SAVE_FLAG);
super.dispatchDraw(canvas);
if (mStrokeWidth > 0) {
// 支持半透明描边,将与描边区域重叠的内容裁剪掉
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
mPaint.setColor(Color.WHITE);
mPaint.setStrokeWidth(mStrokeWidth * 2);
mPaint.setStyle(Paint.Style.STROKE);
canvas.drawPath(mClipPath, mPaint);
// 绘制描边
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER));
mPaint.setColor(mStrokeColor);
mPaint.setStyle(Paint.Style.STROKE);
canvas.drawPath(mClipPath, mPaint);
}
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
mPaint.setColor(Color.WHITE);
mPaint.setStyle(Paint.Style.FILL);
canvas.drawPath(mClipPath, mPaint);
mRCHelper.onClipDraw(canvas);
canvas.restore();
}

@Override public void draw(Canvas canvas) {
canvas.save();
canvas.clipPath(mClipPath);
super.draw(canvas);
canvas.restore();
@Override
public void draw(Canvas canvas) {
if (mRCHelper.mClipBackground) {
canvas.save();
canvas.clipPath(mRCHelper.mClipPath);
super.draw(canvas);
canvas.restore();
} else {
super.draw(canvas);
}
}

@Override public boolean dispatchTouchEvent(MotionEvent ev) {
if (!mAreaRegion.contains((int) ev.getX(), (int) ev.getY())) {
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (!mRCHelper.mAreaRegion.contains((int) ev.getX(), (int) ev.getY())) {
return false;
}
return super.dispatchTouchEvent(ev);
Expand Down
Loading

0 comments on commit 7390401

Please sign in to comment.