版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qiantanlong/article/details/82348006
此篇是自定义ViewGroup实现子View的padding、margin属性(https://blog.csdn.net/qiantanlong/article/details/82347850)的姊妹篇,在于实现自定义View(不包含子View)的padding属性,话不多说,直接看代码。
package hongzhen.com.defineviewdemo;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
/**
* 开发者:hongzhen
* 创建时间: 2018/9/3 13:54
* 公司名称:
* 类名:MyMarginViewGroup.java
* 描述:实现View的padding属性的支持
*/
public class MyPaddingView extends View {
private int mNomalSize=200;
private int mWidth;
private int mHeith;
private int paddingLeft;
private int paddingTop;
private int paddingRight;
private int paddingBottom;
public MyPaddingView(Context context) {
this(context,null);
}
public MyPaddingView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs,0);
}
public MyPaddingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr,0);
}
public MyPaddingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
private void init() {
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
float hScale = 1.0f;
float vScale = 1.0f;
if (widthMode != MeasureSpec.UNSPECIFIED && widthSize < mNomalSize) {
hScale = (float) widthSize / (float) mNomalSize;
}
if (heightMode != MeasureSpec.UNSPECIFIED && heightSize < mNomalSize) {
vScale = (float) heightSize / (float) mNomalSize;
}
float scale = Math.min(hScale, vScale);
mWidth = resolveSizeAndState((int) (mNomalSize * scale), widthMeasureSpec, 0);
mHeith = resolveSizeAndState((int) (mNomalSize * scale), heightMeasureSpec, 0);
setMeasuredDimension(mWidth, mHeith);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint mPaint = new Paint();
mPaint.setColor(Color.RED);
//考虑自定义view设置的padding属性,由于layout_margin相关属性是viewgroup负责的,因此只有自定义
//viewgroup时才需要考虑其子view的layout_margin熟悉生效,view无需考虑layout_margin属性
paddingLeft = getPaddingLeft();
paddingTop = getPaddingTop();
paddingRight = getPaddingRight();
paddingBottom = getPaddingBottom();
Rect rect = new Rect();
rect.left = 0 + paddingLeft;
rect.top = 0 + paddingTop;
rect.right = mWidth - paddingRight;
rect.bottom = mHeith - paddingBottom;
canvas.drawRect(rect,mPaint);
}
}
布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<hongzhen.com.defineviewdemo.MyPaddingView
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_margin="10dp"
android:padding="10dp"/>
<hongzhen.com.defineviewdemo.MyMarginViewGroup
android:background="#aaa"
android:padding="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<hongzhen.com.defineviewdemo.MyPaddingView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ddd"
android:padding="10dp"
android:layout_margin="15dp"/>
<hongzhen.com.defineviewdemo.MyPaddingView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ddd"
android:padding="10dp"
android:layout_margin="15dp"/>
</hongzhen.com.defineviewdemo.MyMarginViewGroup>
</LinearLayout>
效果:请忽略下方的MyMarginViewGroup控件