您当前的位置: 首页 >  android

lichong951

暂无认证

  • 1浏览

    0关注

    131博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

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

lichong951 发布时间:2022-07-25 16:50:10 ,浏览量:1

在这里插入图片描述 自定义装饰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();
    }
}

布局如下:



    

    

    


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 arrayAdapter=new ArrayAdapter(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

关注
打赏
1659512212
查看更多评论
立即登录/注册

微信扫码登录

0.0818s