方法
使用ImageView可以有同样效果,现在我们来看看自定义的TrumpView如何实现
首先新建一个App项目,默认有MainActivity和activity_main.xml
然后将xml文件修改如下
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<com.example.makeview.TrumpView
android:id="@+id/circle_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
当然,此时你的TrumpView会是红色的,
注意
新建一个Trump.java类继承View ,定义一个构造函数如下(全部代码放在文末)
public TrumpView(Context context, AttributeSet attributeSet) {
super(context,attributeSet);
mBitmap=BitmapFactory.decodeResource(context.getResources(),R.drawable.trump);
init();
}
这里将 trump.png文件通过bitmap工厂创建到mBitmap中,这里的init()我只做了简单的处理,设置画笔颜色(如果我们画的图形),但是我们显示图片的话,画笔的颜色不重要
接下来是将图片画到指定区域
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Rect src = new Rect(0, 0, mBitmap.getWidth(), mBitmap.getHeight());
Rect dst = new Rect(0, 0, mBitmap.getWidth(), mBitmap.getHeight());
canvas.drawBitmap(mBitmap,src,dst,mPaint);
}
src 表示从图片上取出得到区域,dst表示在控件中放置画的区域,都选择bitmap的宽高
最后进行尺寸测量,因为自定的View默认是无法显示
android:layout_width="wrap_content"
android:layout_height="wrap_content"
他会将wrap_content直接按match_content处理,所以我们在重写measure的时候,需要设置默认的wrap_content值,这里设置为800
@Override
protected void onMeasure(int widthMeasureSpec,int heightMeasureSpec){
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
if (widthSpecMode == MeasureSpec.AT_MOST && heightMeasureSpec == MeasureSpec.AT_MOST) {
setMeasuredDimension(800, 800);
} else if (widthSpecMode == MeasureSpec.AT_MOST) {
setMeasuredDimension(800, heightSpecSize);
} else if (heightMeasureSpec == MeasureSpec.AT_MOST) {
setMeasuredDimension(widthSpecSize,800);
}
}
这样所有代码都大功告成了!
效果如图
以下是源代码
package com.example.makeview;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
public class TrumpView extends View {
final String TAG = "CircleView";
private int mColor =Color.RED;
private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
Bitmap mBitmap;
public TrumpView(Context context) {
super(context);
Log.d(TAG, "TrumpView: First");
init();
}
public TrumpView(Context context, AttributeSet attributeSet) {
super(context,attributeSet);
Log.d(TAG, "TrumpView: Second");
mBitmap=BitmapFactory.decodeResource(context.getResources(),R.drawable.trump);
init();
}
public TrumpView(Context context, AttributeSet attributeSet, int defStyleAttr) {
super(context, attributeSet, defStyleAttr);
Log.d(TAG, "TrumpView: Third");
init();
}
private void init() {
mPaint.setColor(mColor);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Rect src = new Rect(0, 0, mBitmap.getWidth(), mBitmap.getHeight());
Rect dst = new Rect(0, 0, mBitmap.getWidth(), mBitmap.getHeight());
canvas.drawBitmap(mBitmap,src,dst,mPaint);
}
@Override
protected void onMeasure(int widthMeasureSpec,int heightMeasureSpec){
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
if (widthSpecMode == MeasureSpec.AT_MOST && heightMeasureSpec == MeasureSpec.AT_MOST) {
setMeasuredDimension(800, 800);
} else if (widthSpecMode == MeasureSpec.AT_MOST) {
setMeasuredDimension(800, heightSpecSize);
} else if (heightMeasureSpec == MeasureSpec.AT_MOST) {
setMeasuredDimension(widthSpecSize,800);
}
}
}
网友评论