欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android自定義View實(shí)現(xiàn)簡(jiǎn)單文字描邊功能

 更新時(shí)間:2018年12月12日 16:25:22   作者:張雨明  
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)簡(jiǎn)單文字描邊功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Android實(shí)現(xiàn)簡(jiǎn)單文字描邊功能的具體代碼,供大家參考,具體內(nèi)容如下

效果圖:

實(shí)現(xiàn)代碼:

package com.example.zhangyu.myview.widget;

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.Picture;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.View;

import com.example.zhangyu.myview.R;

public class TouchPullView extends View {


 //圓的半徑
 private float mCircleRadius;
 private Paint paint;
 private Rect rect=new Rect();
 private int count;


 public TouchPullView(Context context) {
  super(context);
  init();
 }


 public TouchPullView(Context context, @Nullable AttributeSet attrs) {
  super(context, attrs);
  init();
 }

 public TouchPullView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
  super(context, attrs, defStyleAttr);
  init();
 }

 @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
 public TouchPullView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
  super(context, attrs, defStyleAttr, defStyleRes);
  init();
 }

 /**
  * 初始化
  */
 private void init() {
  paint = new Paint(Paint.ANTI_ALIAS_FLAG);
  //設(shè)置抗鋸齒
  paint.setAntiAlias(true);
  //設(shè)置防抖動(dòng)
  paint.setDither(true);
  //設(shè)置填充方式
  paint.setStyle(Paint.Style.FILL_AND_STROKE);

  //View的點(diǎn)擊事件
  setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    count++;
    invalidate();//重新繪制
   }
  });

 }

 @Override
 protected void onDraw(Canvas canvas) {
  super.onDraw(canvas);


  //設(shè)置方形畫(huà)筆,背景。
  paint.setColor(Color.YELLOW);
  //先繪制底板
  canvas.drawRect(0, 0, getWidth(), getHeight(), paint);


  float x = getWidth() / 2;
  float y = getHeight() / 2;
  float offset = getWidth()/6;

  //繪制圓形
  paint.setColor(Color.GRAY);
  mCircleRadius = offset*1.41f;
  canvas.drawCircle(x, y, mCircleRadius, paint);

  //繪制圖片
  Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.pic1);
  if (bitmap != null) {
   canvas.drawBitmap(bitmap,null,new RectF(x-offset,y-offset,x+offset,y+offset),paint);
  }
  

  //繪制文字外層的文字,邊框
  String s = String.valueOf(count);
  paint.setStrokeWidth(10);
  paint.setColor(Color.RED);
  paint.setTextSize(100);
  paint.getTextBounds(s,0,s.length(),rect);
  float textWidth = rect.width();
  float textHeight = rect.height();
  x = getWidth()/2-textWidth/2;
  y = getHeight() / 2+textHeight/2;
  canvas.drawText(s, x, y, paint);

  //繪制文字
  paint.setColor(Color.BLUE);
  paint.setStrokeWidth(0);
  canvas.drawText(s, x, y, paint);

 }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論