Created
June 21, 2018 08:18
-
-
Save JeffZane/80d305e34f9fedda81cfb87652ae9e63 to your computer and use it in GitHub Desktop.
Paint stroke width half
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import android.content.Context; | |
| import android.graphics.Canvas; | |
| import android.graphics.Color; | |
| import android.graphics.Paint; | |
| import android.graphics.RectF; | |
| import android.support.annotation.Nullable; | |
| import android.util.AttributeSet; | |
| import android.view.View; | |
| import com.nestia.android.uikit.UiUtils; | |
| import com.nestia.living.base.utils.ScreenUtils; | |
| public class RectView extends View { | |
| private Paint mPaint; | |
| private RectF mRectF; | |
| private RectF mRectF2; | |
| private static final int STROKE_WIDTH = 20; | |
| public RectView(Context context) { | |
| super(context); | |
| init(context); | |
| } | |
| public RectView(Context context, @Nullable AttributeSet attrs) { | |
| super(context, attrs); | |
| init(context); | |
| } | |
| public RectView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { | |
| super(context, attrs, defStyleAttr); | |
| init(context); | |
| } | |
| public RectView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) { | |
| super(context, attrs, defStyleAttr, defStyleRes); | |
| init(context); | |
| } | |
| private void init(Context context) { | |
| mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); | |
| mPaint.setStyle(Paint.Style.STROKE); | |
| mPaint.setStrokeWidth(STROKE_WIDTH); | |
| mRectF = new RectF(0, 0, 500, UiUtils.dp2px(context, 100) - STROKE_WIDTH / 2); | |
| mRectF2 = new RectF(500 + STROKE_WIDTH, STROKE_WIDTH / 2, | |
| ScreenUtils.getScreenWidth() - STROKE_WIDTH / 2, UiUtils.dp2px(context, 100) - STROKE_WIDTH / 2); | |
| } | |
| @Override | |
| protected void onDraw(Canvas canvas) { | |
| mPaint.setColor(Color.parseColor("#ff0000")); | |
| canvas.drawRect(mRectF, mPaint); | |
| mPaint.setColor(Color.parseColor("#00ff00")); | |
| canvas.drawRect(mRectF2, mPaint); | |
| } | |
| } | |
| Activity layout: | |
| <?xml version="1.0" encoding="utf-8"?> | |
| <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
| android:layout_width="match_parent" | |
| android:layout_height="match_parent" | |
| android:background="@color/white"> | |
| <com.nestia.android.poi.view.RectView | |
| android:layout_width="match_parent" | |
| android:layout_height="wrap_content" /> | |
| <View | |
| android:layout_width="match_parent" | |
| android:layout_height="200dp" | |
| android:layout_marginTop="100dp" | |
| android:background="@color/nb_black_translucent" /> | |
| </RelativeLayout> |
Author
JeffZane
commented
Jun 21, 2018

Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment