【Android】ImageView图片装饰 文字、水印、边框(94/100)

在这里插入图片描述
自定义装饰ImageView类:DecorateImageView

public class DecorateImageView extends ImageView {
    private Paint mPaint=new Paint();
    private int mWidth,mHeight;
    private int mTextSize=30;
    private String mText;//文字
    private Bitmap mLogo;//水印
    private Bitmap mFrame;//边框

    public DecorateImageView(Context context) {
        super(context);
    }

    public DecorateImageView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mPaint.setColor(Color.parseColor("#8800FF"));
        mPaint.setTextSize(Utils.dp2px(context,mTextSize));
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidth=getMeasuredWidth();
//        mHeight=getMinimumHeight();
        mHeight=300*2;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(mFrame !=null){
            canvas.drawBitmap(mFrame,null,new Rect(0,0,mWidth,mHeight),mPaint);
        }
        if(!TextUtils.isEmpty(mText)){
            int textHeight= (int)MeasureUtil.getTextHeight(mText,mTextSize);
            canvas.drawText(mText,0,Math.abs(mHeight-textHeight),mPaint);
        }
        if(null!=mLogo){
            canvas.drawBitmap(mLogo,mWidth-mLogo.getWidth()
                    ,mHeight-mLogo.getHeight()
                    ,mPaint);
        }
    }

    public void showNone(){
        mText="";
        mLogo=null;
        mFrame=null;
        postInvalidate();
    }

    public void showText(String text,boolean isReset){
        if(isReset){
            showNone();
        }
        mText=text;
        postInvalidate();
    }

    public void showLogo(Bitmap bitmap,boolean isReset){
        if(isReset){
            showNone();
        }
        mLogo=bitmap;
        postInvalidate();
    }

    public void showFrame(Bitmap bitmap,boolean isReset){
        if(isReset){
            showNone();
        }
        mFrame=bitmap;
        postInvalidate();
    }
}

布局如下:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity.DecorateImageViewActivity">
    <TextView
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:id="@+id/tv_label"
        android:text="图像装饰类型:"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <Spinner
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toEndOf="@id/tv_label"
        app:layout_constraintEnd_toEndOf="parent"
        android:id="@+id/spinner"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:spinnerMode="dialog"
        />

    <top.lc951.myandroid.views.DecorateImageView
        android:id="@+id/iv_decorate"

        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tv_label"

        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:src="@mipmap/ic_img01"
        android:scaleType="fitXY"
        android:layout_margin="10dp"
        />

</android.support.constraint.ConstraintLayout>

Activity控制类

package top.lc951.myandroid.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import top.lc951.myandroid.R;
import top.lc951.myandroid.views.DecorateImageView;

/**
 * 图片装饰 文字、水印、边框
 * 参考:{@link DecorateImageView}
 */
public class DecorateImageViewActivity extends AppCompatActivity {

    private DecorateImageView decorateImageView;

    public static void actionActivity(Context context) {
        Intent intent = new Intent(context, DecorateImageViewActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_decorate_image_view);
        decorateImageView=findViewById(R.id.iv_decorate);
        initDecorateSpinner();
    }
    private String[] decorateNameArray = {"无装饰", "文字", "图片水印", "相框"};
    private void initDecorateSpinner() {
        ArrayAdapter<String> arrayAdapter=new ArrayAdapter<String>(this
                ,android.R.layout.simple_spinner_item
                ,decorateNameArray);
        Spinner spinner=findViewById(R.id.spinner);
        spinner.setPrompt("请选择装饰类型");
        spinner.setAdapter(arrayAdapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                showResult(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        spinner.setSelection(0);
    }

    private void showResult(int position) {
        switch (position){
            case 0:
                decorateImageView.showNone();
                break;
            case 1:
                decorateImageView.showText("@lichong951",true);
                break;
            case 2:
                Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.lichong951);
                decorateImageView.showLogo(bitmap,true);
                break;
            case 3:
                Bitmap frame = BitmapFactory.decodeResource(getResources(), R.mipmap.photo_frame3);
                decorateImageView.showFrame(frame,true);
                break;
            default:
                break;
        }
    }


}

工具类:


/**
 * @author lichong
 * 2022年07月25日14:48:47
 */
public class MeasureUtil {

    /**
     * 测量文字的宽度
     * */
    public static float measureTextWidth(String txt,int size){
        Paint paint=new Paint();
        paint.setTextSize(size);
        return paint.measureText(txt);
    }

    public static Rect measureTextRectByBounds(String str){
        Paint paint = new Paint();
        Rect rect = new Rect();
        paint.getTextBounds(str, 0, str.length(), rect);
        int w = rect.width();
        int h = rect.height();
        return rect;
    }

    public static int measureTextWidthByBounds(String str){
        return measureTextRectByBounds(str).width();
    }
    public static int measureTextHeightByBounds(String str){
        return measureTextRectByBounds(str).height();
    }

    // 获取指定文本的高度
    public static float getTextHeight(String text, float textSize) {
        Paint paint = new Paint(); // 创建一个画笔对象
        paint.setTextSize(textSize); // 设置画笔的文本大小
        Paint.FontMetrics fm = paint.getFontMetrics(); // 获取画笔默认字体的度量衡
        return fm.descent - fm.ascent; // 返回文本自身的高度
        //return fm.bottom - fm.top + fm.leading;  // 返回文本所在行的行高
    }

扩展:

Canvas指画布,表现在屏幕上就是一块区域,可以在上面使用各种API绘制想要的东西
Canvas的坐标系:

画布以左上角为原点(0,0),向右为X轴的正方向,向下为Y轴的正方向

Canvas的绘图操作:

绘制颜色 drawColor、drawRGB、drawARGB

绘制圆 drawCircle

绘制点 drawPoint

绘制直线 drawLine

绘制矩形 drawRect

绘制圆角矩形 drawRoundRect

绘制椭圆 drawOval

绘制弧形 drawArc

绘制文本 drawText

沿Path路径绘制文本 drawTextOnPath

绘制位图 drawBitmap

//绘制颜色
public void drawColor(@ColorInt int color)
public void drawRGB(int r, int g, int b)
public void drawARGB(int a, int r, int g, int b)
 
//绘制圆
public void drawCircle(float cx, float cy, float radius, @NonNull Paint paint)
 
//绘制点
public void drawPoint(float x, float y, @NonNull Paint paint)
//绘制多个点
public void drawPoints(@Size(multiple = 2) @NonNull float[] pts, @NonNull Paint paint)
 
//绘制一条直线
public void drawLine(float startX, float startY, float stopX, float stopY, @NonNull Paint paint)
//绘制多条直线
public void drawLines(@Size(multiple = 4) @NonNull float[] pts, @NonNull Paint paint)
 
//绘制一个矩形
public void drawRect(@NonNull RectF rect, @NonNull Paint paint)
public void drawRect(@NonNull Rect r, @NonNull Paint paint)
public void drawRect(float left, float top, float right, float bottom, @NonNull Paint paint)
 
//绘制一个圆角矩形
public void drawRoundRect(@NonNull RectF rect, float rx, float ry, @NonNull Paint paint)
public void drawRoundRect(float left, float top, float right, float bottom, float rx, float ry, @NonNull Paint paint)
 
//绘制一个椭圆
public void drawOval(@NonNull RectF oval, @NonNull Paint paint)
public void drawOval(float left, float top, float right, float bottom, @NonNull Paint paint)
 
//绘制一个弧形
public void drawArc(@NonNull RectF oval, float startAngle, float sweepAngle, boolean useCenter, @NonNull Paint paint)
public void drawArc(float left, float top, float right, float bottom, float startAngle, float sweepAngle, boolean useCenter, @NonNull Paint paint)
 
//绘制文本
public void drawText(@NonNull String text, float x, float y, @NonNull Paint paint)
//沿Path路径绘制文本
public void drawTextOnPath(@NonNull String text, @NonNull Path path, float hOffset, float vOffset, @NonNull Paint paint)
 
//绘制位图
public void drawBitmap(@NonNull Bitmap bitmap, float left, float top, @Nullable Paint paint)
public void drawBitmap(@NonNull Bitmap bitmap, @Nullable Rect src, @NonNull RectF dst, @Nullable Paint paint)

参考:
https://blog.csdn.net/zenmela2011/article/details/123458194

https://blog.csdn.net/xujian197/article/details/79903544

自研产品推荐

历时一年半多开发终于smartApi-v1.0.0版本在2023-09-15晚十点正式上线
smartApi是一款对标国外的postman的api调试开发工具,由于开发人力就作者一个所以人力有限,因此v1.0.0版本功能进行精简,大功能项有:

  • api参数填写
  • api请求响应数据展示
  • PDF形式的分享文档
  • Mock本地化解决方案
  • api列表数据本地化处理
  • 再加上UI方面的打磨

为了更好服务大家把之前的公众号和软件激活结合,如有疑问请大家反馈到公众号即可,下个版本30%以上的更新会来自公众号的反馈。
嗯!先解释不上服务端原因,API调试工具的绝大多数时候就是一个数据模型、数据处理、数据模型理解共识的问题解决工具,所以作者结合自己十多年开发使用的一些痛点来打造的,再加上服务端开发一般是面向企业的,作者目前没有精力和时间去打造企业服务。再加上没有资金投入所以服务端开发会滞后,至于什么时候会进行开发,这个要看募资情况和用户反馈综合考虑。虽然目前国内有些比较知名的api工具了,但作者使用后还是觉得和实际使用场景不符。如果有相关吐槽也可以在作者的公众号里反馈蛤!
下面是一段smartApi使用介绍:
在这里插入图片描述

下载地址:

https://pan.baidu.com/s/1iultkXqeLNG4_eNiefKTjQ?pwd=cnbl

posted @ 2022-07-25 16:50  lichong951  阅读(24)  评论(0编辑  收藏  举报  来源