[Java] 高仿微信java代码 →→→→→进入此内容的聊天室

来自 , 2020-06-30, 写在 Java, 查看 129 次.
URL http://www.code666.cn/view/2cb6b103
  1. ackage com.example.mofangweixin;
  2.  
  3. import android.content.Context;
  4. import android.content.res.TypedArray;
  5. import android.graphics.Bitmap;
  6. import android.graphics.Bitmap.Config;
  7. import android.graphics.Canvas;
  8. import android.graphics.Paint;
  9. import android.graphics.PorterDuff;
  10. import android.graphics.PorterDuffXfermode;
  11. import android.graphics.Rect;
  12. import android.graphics.drawable.BitmapDrawable;
  13. import android.os.Bundle;
  14. import android.os.Looper;
  15. import android.os.Parcelable;
  16. import android.util.AttributeSet;
  17. import android.util.TypedValue;
  18. import android.view.View;
  19.  
  20. public class ChangeColorIconWithText extends View {
  21.  
  22.         private int mColor = 0xFF6495ED;
  23.         private Bitmap mIconBitmap;
  24.         private String mText = "微信";
  25.         private int mTextSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 12,
  26.                         getResources().getDisplayMetrics());
  27.  
  28.         private Canvas mCanvas;
  29.         private Bitmap mBitmap;
  30.         private Paint mPaint;
  31.  
  32.         private float mAlpha;
  33.  
  34.         private Rect mIconRect;
  35.         private Rect mTextBound;
  36.         private Paint mTextPaint;
  37.  
  38.         public ChangeColorIconWithText(Context context) {
  39.                 this(context, null);
  40.         }
  41.  
  42.         public ChangeColorIconWithText(Context context, AttributeSet attrs) {
  43.                 this(context, attrs, 0);
  44.         }
  45.  
  46.         /**
  47.          * 获取自定义属性的值
  48.          *
  49.          * @param context
  50.          * @param attrs
  51.          * @param defStyleAttr
  52.          */
  53.         public ChangeColorIconWithText(Context context, AttributeSet attrs, int defStyleAttr) {
  54.                 super(context, attrs, defStyleAttr);
  55.  
  56.                 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ChangeColorIconWithText);
  57.  
  58.                 int n = a.getIndexCount();
  59.  
  60.                 for (int i = 0; i < n; i++) {
  61.                         int attr = a.getIndex(i);
  62.                         switch (attr) {
  63.                         case R.styleable.ChangeColorIconWithText_icon:
  64.                                 BitmapDrawable drawable = (BitmapDrawable) a.getDrawable(attr);
  65.                                 mIconBitmap = drawable.getBitmap();
  66.                                 break;
  67.                         case R.styleable.ChangeColorIconWithText_color:
  68.                                 mColor = a.getColor(attr, 0xFF191970);
  69.                                 break;
  70.                         case R.styleable.ChangeColorIconWithText_text:
  71.                                 mText = a.getString(attr);
  72.                                 break;
  73.                         case R.styleable.ChangeColorIconWithText_text_size:
  74.                                 mTextSize = (int) a.getDimension(attr,
  75.                                                 TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 12, getResources().getDisplayMetrics()));
  76.                                 break;
  77.                         }
  78.  
  79.                 }
  80.  
  81.                 a.recycle();
  82.  
  83.                 mTextBound = new Rect();
  84.                 mTextPaint = new Paint();
  85.                 mTextPaint.setTextSize(mTextSize);
  86.                 mTextPaint.setColor(0XFF6495ED);
  87.                 mTextPaint.getTextBounds(mText, 0, mText.length(), mTextBound);
  88.  
  89.         }
  90.  
  91.         @Override
  92.         protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  93.                 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  94.                 int iconWidth = Math.min(getMeasuredWidth() - getPaddingLeft() - getPaddingRight(),
  95.                                 getMeasuredHeight() - getPaddingTop() - getPaddingBottom() - mTextBound.height());
  96.  
  97.                 int left = getMeasuredWidth() / 2 - iconWidth / 2;
  98.                 int top = getMeasuredHeight() / 2 - (mTextBound.height() + iconWidth) / 2;
  99.                 mIconRect = new Rect(left, top, left + iconWidth, top + iconWidth);
  100.         }
  101.  
  102.         @Override
  103.         protected void onDraw(Canvas canvas) {
  104.                 canvas.drawBitmap(mIconBitmap, null, mIconRect, null);
  105.  
  106.                 int alpha = (int) Math.ceil(255 * mAlpha);
  107.  
  108.                 // 内存去准备mBitmap , setAlpha , 纯色 ,xfermode , 图标
  109.                 setupTargetBitmap(alpha);
  110.                 // 1、绘制原文本 ; 2、绘制变色的文本
  111.                 drawSourceText(canvas, alpha);
  112.                 drawTargetText(canvas, alpha);
  113.  
  114.                 canvas.drawBitmap(mBitmap, 0, 0, null);
  115.  
  116.         }
  117.  
  118.         /**
  119.          * 绘制变色的文本
  120.          *
  121.          * @param canvas
  122.          * @param alpha
  123.          */
  124.         private void drawTargetText(Canvas canvas, int alpha) {
  125.                 mTextPaint.setColor(mColor);
  126.                 mTextPaint.setAlpha(alpha);
  127.                 int x = getMeasuredWidth() / 2 - mTextBound.width() / 2;
  128.                 int y = mIconRect.bottom + mTextBound.height();
  129.                 canvas.drawText(mText, x, y, mTextPaint);
  130.  
  131.         }
  132.  
  133.         /**
  134.          * 绘制原文本
  135.          *
  136.          * @param canvas
  137.          * @param alpha
  138.          */
  139.         private void drawSourceText(Canvas canvas, int alpha) {
  140.                 //底部字体的颜色
  141.                 mTextPaint.setColor(0xFF696969);
  142.                 mTextPaint.setAlpha(255 - alpha);
  143.                 int x = getMeasuredWidth() / 2 - mTextBound.width() / 2;
  144.                 int y = mIconRect.bottom + mTextBound.height();
  145.                 canvas.drawText(mText, x, y, mTextPaint);
  146.  
  147.         }
  148.  
  149.         /**
  150.          * 在内存中绘制可变色的Icon
  151.          */
  152.         private void setupTargetBitmap(int alpha) {
  153.                 mBitmap = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(), Config.ARGB_8888);
  154.                 mCanvas = new Canvas(mBitmap);
  155.                 mPaint = new Paint();
  156.                 mPaint.setColor(mColor);
  157.                 mPaint.setAntiAlias(true);
  158.                 mPaint.setDither(true);
  159.                 mPaint.setAlpha(alpha);
  160.                 mCanvas.drawRect(mIconRect, mPaint);
  161.                 mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
  162.                 mPaint.setAlpha(255);
  163.                 mCanvas.drawBitmap(mIconBitmap, null, mIconRect, mPaint);
  164.         }
  165.  
  166.         private static final String INSTANCE_STATUS = "instance_status";
  167.         private static final String STATUS_ALPHA = "status_alpha";
  168.  
  169.         @Override
  170.         protected Parcelable onSaveInstanceState() {
  171.                 Bundle bundle = new Bundle();
  172.                 bundle.putParcelable(INSTANCE_STATUS, super.onSaveInstanceState());
  173.                 bundle.putFloat(STATUS_ALPHA, mAlpha);
  174.                 return bundle;
  175.         }
  176.  
  177.         @Override
  178.         protected void onRestoreInstanceState(Parcelable state) {
  179.                 if (state instanceof Bundle) {
  180.                         Bundle bundle = (Bundle) state;
  181.                         mAlpha = bundle.getFloat(STATUS_ALPHA);
  182.                         super.onRestoreInstanceState(bundle.getParcelable(INSTANCE_STATUS));
  183.                         return;
  184.                 }
  185.                 super.onRestoreInstanceState(state);
  186.         }
  187.  
  188.         public void setIconAlpha(float alpha) {
  189.                 this.mAlpha = alpha;
  190.                 invalidateView();
  191.         }
  192.  
  193.         /**
  194.          * 重绘
  195.          */
  196.         private void invalidateView() {
  197.                 if (Looper.getMainLooper() == Looper.myLooper()) {
  198.                         invalidate();
  199.                 } else {
  200.                         postInvalidate();
  201.                 }
  202.         }
  203.  
  204. }
  205.  

回复 "高仿微信java代码"

这儿你可以回复上面这条便签

captcha