我正在制作自定义视图应用程序。我想在onDraw画布上设置paint的单个部分(Tap to start),这是我的代码,我从互联网调整了源代码并将其放入我的应用程序中。
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
public class CustomView extends ImageView{
private static Paint paint;
private int w;
private int h;
private Animation scaleAnim;
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
//To customize the brown border
paint = new Paint();
paint.setStrokeWidth(20);
paint.setColor(Color.rgb(123, 92, 13));
paint.setStyle(Paint.Style.STROKE);
}
//To fit with the situation of vertical and horizontal orientation
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
this.w = w;
this.h = h;
super.onSizeChanged(w, h, oldw, oldh);
}
private void createAnim(Canvas canvas) {
scaleAnim = new ScaleAnimation(0f,1f,0f,1f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
scaleAnim.setRepeatMode(Animation.REVERSE);
scaleAnim.setRepeatCount(Animation.INFINITE);
scaleAnim.setDuration(10000L);
scaleAnim.setInterpolator(new AccelerateDecelerateInterpolator());
startAnimation(scaleAnim);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// creates the animation the first time
if (scaleAnim == null) {
createAnim(canvas);
}
//To obtain the height and width
int height = this.getMeasuredHeight();
int width = this.getMeasuredWidth();
//To draw the dark green background
canvas.drawColor(Color.rgb(15, 37, 7));
//To paint the brown border
canvas.drawRect(10, 10, width - 10, height - 10, paint);
//To obtain the image
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.main_icon);
//To place the image on the right bottom corner
canvas.drawBitmap(bmp, canvas.getWidth()-bmp.getWidth(), canvas.getHeight()-bmp.getHeight() , paint);
//To obtain the custom font and make it available to use
Typeface typeface = Typeface.createFromAsset(getContext().getAssets(),
"fonts/KG.ttf");
//To create a new paint style for text
Paint textStyle = new Paint();
//Set the font style
textStyle.setTypeface(typeface);
//To set text color to white
textStyle.setColor(Color.rgb(255, 255, 255));
//To set text size
textStyle.setTextSize(100);
//To make text centered
textStyle.setTextAlign(Align.CENTER);
//To print the text as main title with style specified
canvas.drawText("L E A R N E R !", w/2, h/2, textStyle);
//To create a new paint and customize them
Paint jpStyle = new Paint();
jpStyle.setColor(Color.rgb(252,71,71));
jpStyle.setTextSize(50);
jpStyle.setTextAlign(Align.CENTER);
jpStyle.setTypeface(typeface);
canvas.drawText("@ J A P A N", w/2, h/2+50, jpStyle);
//To create a new paint and customize them
Paint startStyle = new Paint();
startStyle.setColor(Color.WHITE);
startStyle.setTextSize(40);
startStyle.setTextAlign(Align.CENTER);
startStyle.setTypeface(typeface);
canvas.drawText("> Tap to Start <", w/2, h/2+170, startStyle);
}
}