当前位置:   article > 正文

Android开发自定义水印图片_android 照片水印 模版

android 照片水印 模版

上一篇的自定义View中的测量绘制 http://blog.csdn.net/jinjin10086/article/details/54947301

在此基础上,本次在此基础上继续进行学习。

本次的目标是在上一篇的基础上给普通的图片加上水印,实际的应用如csdn上传图片、微信公众号上传图片,都会自动给图片加上水印,当然本次只是给加上水印,可设置水印的颜色,内容,规格大小(small为14号字体,normal为16号字体,big为18号字体)等。

实现的效果如下:

接下来解析具体得实现过程:

1)和上次的一样,继承自View重写其构造方法,如下:

  1. public DemoImageView01(Context context) {
  2. this(context,null);
  3. }
  4. public DemoImageView01(Context context, AttributeSet attrs) {
  5. this(context, attrs,0);
  6. }
  7. public DemoImageView01(Context context, AttributeSet attrs, int defStyleAttr) {
  8. super(context, attrs, defStyleAttr);
  9. }
2)自定义属性如下:

    1,属性的自定义内容:

  1. <declare-styleable name="DemoImageView01">
  2. <attr name="image" format="reference"/>
  3. <attr name="watermark" format="string"/>
  4. <attr name="watermarkcolor" format="color"/>
  5. <attr name="watermarksize">
  6. <enum name="small" value="0" />
  7. <enum name="normal" value="1" />
  8. <enum name="big" value="2" />
  9. </attr>
  10. </declare-styleable>
2,取到相关的属性如下(对于图片资源进行直接解析):

  1. TypedArray array = context.getTheme().obtainStyledAttributes(attrs, R.styleable.DemoImageView01,defStyleAttr,0);
  2. for (int i = 0; i < array.getIndexCount(); i++) {
  3. int attr = array.getIndex(i);
  4. switch (attr){
  5. case R.styleable.DemoImageView01_image:
  6. Log.d("test","image-->" + array.getResourceId(attr,0));
  7. mImageSrc = BitmapFactory.decodeResource(getResources(),array.getResourceId(attr,0));
  8. break;
  9. case R.styleable.DemoImageView01_watermark:
  10. mWatermark = array.getString(attr);
  11. break;
  12. case R.styleable.DemoImageView01_watermarkcolor:
  13. mWatermarkColor = array.getColor(attr, Color.BLUE);
  14. break;
  15. case R.styleable.DemoImageView01_watermarksize:
  16. int style = array.getInt(attr,0);
  17. switch (style){
  18. case 0:
  19. mWatermarkSize = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 14, getResources().getDisplayMetrics());
  20. break;
  21. case 1:
  22. mWatermarkSize = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics());
  23. break;
  24. case 2:
  25. mWatermarkSize = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 18, getResources().getDisplayMetrics());
  26. break;
  27. default:
  28. break;
  29. }
  30. break;
  31. default:
  32. break;
  33. }
  34. }



3)初始化画笔及绘制范围如下:

  1. mPaint = new Paint();
  2. mBound = new Rect();
  3. mWaterMarkBound = new Rect();
  4. mPaint.setTextSize(mWatermarkSize);
  5. mPaint.getTextBounds(mWatermark, 0, mWatermark.length(), mWaterMarkBound);
4)测量,重写方法onMeasure如下:

  1. @Override
  2. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  3. int widthMode = MeasureSpec.getMode(widthMeasureSpec);
  4. int widthSize = MeasureSpec.getSize(widthMeasureSpec);
  5. int heightMode = MeasureSpec.getMode(heightMeasureSpec);
  6. int heightSize = MeasureSpec.getSize(heightMeasureSpec);
  7. if (widthMode == MeasureSpec.EXACTLY){
  8. mWidth = widthSize;
  9. }else {
  10. //图片的宽度 (注:水印覆盖在图片上,不占宽度)
  11. int desire = getPaddingLeft() + getPaddingRight() + mImageSrc.getWidth();
  12. mWidth = Math.min(desire,widthSize);
  13. }
  14. if (heightMode == MeasureSpec.EXACTLY){
  15. mHeight = heightSize;
  16. }else {
  17. ///图片的宽度 (注:水印覆盖在图片上,不占高度)
  18. int desire = getPaddingTop() + getPaddingBottom() + mImageSrc.getHeight();
  19. mHeight = Math.min(desire,heightSize);
  20. }
  21. Log.d("test","mWidth" + mWidth);
  22. Log.d("test","mHeight" + mHeight);
  23. setMeasuredDimension(mWidth,mHeight);
  24. }
5)绘制,重写onDraw方法如下:

  1. @Override
  2. protected void onDraw(Canvas canvas) {
  3. /*
  4. 整体绘制区域的规定
  5. */
  6. mBound.left = getPaddingLeft();
  7. mBound.right = mWidth - getPaddingRight();
  8. mBound.top = getPaddingTop();
  9. mBound.bottom = mHeight - getPaddingBottom();
  10. mPaint.setStyle(Paint.Style.FILL);
  11. mPaint.setColor(Color.YELLOW);
  12. //绘制图片
  13. canvas.drawBitmap(mImageSrc,null,mBound,mPaint);
  14. mPaint.setTextSize(mWatermarkSize);
  15. mPaint.getTextBounds(mWatermark,0,mWatermark.length(),mWaterMarkBound);
  16. mPaint.setColor(mWatermarkColor);
  17. /*
  18. 如果水印的宽度 大于整体区域的宽度
  19. */
  20. if (mWaterMarkBound.width() > mWidth){
  21. TextPaint paint = new TextPaint(mPaint);
  22. String mWaterMarkNow = TextUtils.ellipsize(mWatermark,paint,(float) (mWidth - getPaddingLeft() - getPaddingRight()), TextUtils.TruncateAt.END).toString();
  23. canvas.drawText(mWaterMarkNow,getPaddingLeft(), mHeight - getPaddingBottom() - mWaterMarkBound.height(), mPaint);
  24. }else {
  25. canvas.drawText(mWatermark, mWidth - mWaterMarkBound.width() - getPaddingRight() , mHeight - getPaddingBottom() - mWaterMarkBound.height(), mPaint);
  26. }
  27. }
6)自定义控件的使用如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:image1="http://schemas.android.com/apk/res-auto"
  5. xmlns:tools="http://schemas.android.com/tools"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. android:gravity="center_horizontal"
  9. android:orientation="vertical"
  10. tools:context="com.jinjin.viewstudy.viewstudy.MainActivity">
  11. <com.jinjin.viewstudy.viewstudy.view.DemoImageView01
  12. android:layout_width="200dp"
  13. android:layout_height="200dp"
  14. android:padding="5dp"
  15. image1:image="@mipmap/image1"
  16. image1:watermark="http://blog.jinjin.net"
  17. image1:watermarkcolor="@android:color/holo_red_dark"
  18. image1:watermarksize="small"/>
  19. <com.jinjin.viewstudy.viewstudy.view.DemoImageView01
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:padding="5dp"
  23. image1:image="@mipmap/lmj"
  24. image1:watermark="http://blog.jinjin1.net"
  25. image1:watermarkcolor="@android:color/holo_red_dark"
  26. image1:watermarksize="big"/>
  27. <com.jinjin.viewstudy.viewstudy.view.DemoImageView01
  28. android:layout_width="wrap_content"
  29. android:layout_height="wrap_content"
  30. android:padding="5dp"
  31. image1:image="@mipmap/image2"
  32. image1:watermark="http://blog.jinjin1.net"
  33. image1:watermarkcolor="@android:color/holo_red_dark"
  34. image1:watermarksize="big"/>
  35. </LinearLayout>


到此,即可实现上述的显示效果。

源码下载:

MyDemo


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/45697
推荐阅读
相关标签
  

闽ICP备14008679号