1. Multi-line drawing
There’s only one core method in there.
package com.xm.wanapp.view; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Paint; import android.text.TextPaint; import android.util.AttributeSet; import android.view.View; import com.xm.wanapp.R; public class TextMultiDrawView extends View { private Paint mPaint; private TextPaint mTextPaint; Private String text = "Australia has questioned the authenticity of Japan's scientific whaling. In 2010, The Australian government filed a lawsuit at the International Court of Justice in The Hague, alleging that Japan's "scientific" whaling in the Southern Ocean was actually commercial. In 2014, the International Court of Justice issued its final ruling, finding that Japan's whaling "for scientific purposes" was not justified and that its whaling was in violation of the International Convention on Whaling Control. Japan said it respected the court's decision and backed off for a while, but the hunt continued. In September 2018, at the IWC's Summit in Brazil, Japan renewed its call to resume commercial whaling, but was again rejected by the committee. This is seen as a direct reason for Japan's eventual withdrawal from the organisation. How do whales, killed for "scientific research", end up on the dinner table? Whales killed in the name of scientific research are sent back to Japan to feed the population. The body responsible for this is the Japan Institute of Cetacean Research, a subsidiary of Japan's Fisheries Agency. Japan's Institute of Cetacean Research has a funny name for whale meat: a byproduct of scientific investigation. They say that under Article 8 of the International Convention on the Rules of Whaling, whale bodies should be used to the fullest extent possible. After a whale is caught on a fishing boat and tested for its size, sebum, stomach contents and so on, the whale body is disassembled and used for whale meat consumption. When boats arrive in Japan, chunks of whale meat are distributed to various levels of consumption in Japan, or sold to local governments at well below market prices and served to Japanese schoolchildren for lunch. ; float[] curWidth = new float[1]; private int mScreenHeight; private Bitmap mBitmap; public TextMultiDrawView(Context context) { super(context); init(context); } public TextMultiDrawView(Context context, AttributeSet set) { super(context, set); init(context); } private void init(Context context){ mPaint = new Paint(); mPaint.setTextSize(Utils.dp2px(15)); mTextPaint = new TextPaint(); mTextPaint.setTextSize(Utils.dp2px(15)); mScreenHeight = Utils.getScreenHeight(context); mBitmap = Utils.getBitmapByWidth(context.getResources(), R.mipmap.ic_launcher_round,Utils.dp2px(100)); } @Override protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); Int widthSize = MeasureSpec. GetSize (widthMeasureSpec); Int heightMode = MeasureSpec. GetMode (heightMeasureSpec); UNSPECIFIED (heightMode == MeasureSpec.UNSPECIFIED){setMeasuredDimension(widthSize,mScreenHeight *2); } } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawBitmap(mBitmap,getWidth() - Utils.dp2px(100),100,mPaint); BreakText () //index is the number of strings that can be stored at the moment. * Otherwise, measure backwards from scratch // Parameter three: maximum width to be accumulated // Parameter four: optional. If not null, return the actual width of the measurement * int index = mPaint. BreakText (text,true,getWidth(),curWidth); Canvas. DrawText (text, 0, index, 0100, mPaint); int oldIndex = index; index = mPaint.breakText(text,index,text.length(),true,getWidth() - Utils.dp2px(100),curWidth); Canvas. DrawText (text,oldIndex,oldIndex + index,0,100 + mPaint. GetFontSpacing (); oldIndex = index; index = mPaint.breakText(text,index,text.length(),true,getWidth() - Utils.dp2px(100),curWidth); Canvas. DrawText (text,oldIndex,oldIndex + index,0,100 + mPaint. GetFontSpacing ()*2,mPaint); }}Copy the code
2. Round head drawing
Public class RoundView extends View {/** * private Paint mPaint; /** * private Bitmap SRC; public RoundView(Context context, AttributeSet attrs) { super(context, attrs); // Initialize the paintbrush mPaint = new Paint(); mPaint.setAntiAlias(true); / / get a prototype figure SRC = BitmapFactory. DecodeResource (getResources (), R.d rawable. Timg); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // Set the Color of the canvas to transparent canvas.drawColor(color.red); // This method is equivalent to photoshop creating a new layer, Below you want to do in the action / / https://www.yimipuzi.com/1139.html on this layer canvas. SaveLayer (0, 0, getMeasuredWidth () and getMeasuredHeight (), null, Canvas.ALL_SAVE_FLAG); DrawCircle (getMeasuredWidth() / 2, getMeasuredHeight() / 2, getMeasuredHeight() / 2, mPaint); // Draw the circle you want to display, Canvas. drawCircle(getMeasuredWidth() / 2, getMeasuredHeight() / 2, mPaint); / / set the blend Mode. / / PorterDuff Mode attribute value reference https://www.cnblogs.com/r-decade/p/6250450.html?utm_source=itdadao&utm_medium=referral mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); DrawBitmap (SRC, 0, 0, mPaint); // Restore mixed mode mPaint. SetXfermode (null); // Restore the canvas, equivalent to the merged layer canvas. Restore (); }}Copy the code