activity_main.xml--图片包裹内容
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FFFFFFFF" android:orientation="vertical" > <com.aigestudio.customviewdemo.views.CustomView android:id="@+id/main_pv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="20dp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="sfsdfsd" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="sffdf" /> </LinearLayout>
package com.aigestudio.customviewdemo.activities; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import com.aigestudio.customviewdemo.R; import com.aigestudio.customviewdemo.views.CustomView; public class MainActivity extends Activity { private CustomView mImgView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mImgView = (CustomView) findViewById(R.id.main_pv); Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.lovestory); mImgView.setBitmap(bitmap); } }
package com.aigestudio.customviewdemo.utils; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.Color; import android.util.DisplayMetrics; /** * 测绘工具类 */ public final class MeasureUtil { public static int[] getScreenSize(Activity activity) { DisplayMetrics metrics = new DisplayMetrics(); activity.getWindowManager().getDefaultDisplay().getMetrics(metrics); return new int[] { metrics.widthPixels, metrics.heightPixels }; } }
package com.aigestudio.customviewdemo.views; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.util.AttributeSet; import android.util.Log; import android.view.View; public class CustomView extends View { private Bitmap mBitmap;// 位图对象 public CustomView(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onDraw(Canvas canvas) { // 绘制位图 canvas.drawBitmap(mBitmap, getPaddingLeft(), getPaddingTop(), null); } /** * 设置位图 * * @param bitmap * 位图对象 */ public void setBitmap(Bitmap bitmap) { this.mBitmap = bitmap; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // 声明一个临时变量来存储计算出的测量值 int resultWidth = 0; // 获取宽度测量规格中的mode int modeWidth = MeasureSpec.getMode(widthMeasureSpec); // 获取宽度测量规格中的size int sizeWidth = MeasureSpec.getSize(widthMeasureSpec); /* * 如果爹心里有数 */ if (modeWidth == MeasureSpec.EXACTLY) { // 那么儿子也不要让爹难做就取爹给的大小吧 resultWidth = mBitmap.getWidth() + getPaddingLeft() + getPaddingRight(); } /* * 如果爹心里没数 */ else { // 那么儿子可要自己看看自己需要多大了 resultWidth = mBitmap.getWidth() + getPaddingLeft() + getPaddingRight(); /* * 如果爹给儿子的是一个限制值 */ if (modeWidth == MeasureSpec.AT_MOST) { // 那么儿子自己的需求就要跟爹的限制比比看谁小要谁 resultWidth = Math.min(resultWidth, sizeWidth); } } int resultHeight = 0; int modeHeight = MeasureSpec.getMode(heightMeasureSpec); int sizeHeight = MeasureSpec.getSize(heightMeasureSpec); if (modeHeight == MeasureSpec.EXACTLY) { resultHeight = sizeHeight; } else { resultHeight = mBitmap.getHeight() + getPaddingTop() + getPaddingBottom(); if (modeHeight == MeasureSpec.AT_MOST) { resultHeight = mBitmap.getHeight() + getPaddingTop() + getPaddingBottom(); } } // 设置测量尺寸 setMeasuredDimension(resultWidth, resultHeight); } }
canvas.drawBitmap(mBitmap, getPaddingLeft(), getPaddingTop(), null);
resultWidth = mBitmap.getWidth() + getPaddingLeft() + getPaddingRight();
resultHeight = mBitmap.getHeight() + getPaddingTop() + getPaddingBottom();
效果如下--内边界产生
一个View的大小由其父容器的测量规格MeasureSpec和View本身的布局参数LayoutParams共同决定
ViewGroup中的onLayout方法是一个抽象方法,这意味着你在继承时必须实现,onLayout的目的是为了确定子元素在父容器中的位置,那么这个步骤理应该由父容器来决定而不是子元素,因此,我们可以猜到View中的onLayout方法应该是一个空实现:
如果修改布局--添加margin,首先要知道Margins封装在LayoutParams中(因为父类不是自定义的,所以图片会以左顶点(40,40)开始绘制!!!!!!!!!!!!!代码不用改)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FFFFFFFF" android:orientation="vertical" > <com.example.customview.CustomImage android:id="@+id/main_pv" android:layout_width="wrap_content" android:layout_margin="20dp" android:layout_height="wrap_content" android:padding="20dp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="sfsdfsd" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="sffdf" /> </LinearLayout>