Skip to content

Instantly share code, notes, and snippets.

@JeffZane
Created June 21, 2018 08:18
Show Gist options
  • Select an option

  • Save JeffZane/80d305e34f9fedda81cfb87652ae9e63 to your computer and use it in GitHub Desktop.

Select an option

Save JeffZane/80d305e34f9fedda81cfb87652ae9e63 to your computer and use it in GitHub Desktop.
Paint stroke width half
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>
@JeffZane
Copy link
Author

image

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