Android添加水印效果

来源:互联网 发布:linux 一键脚本ssr 编辑:程序博客网 时间:2024/05/20 14:28

Android添加水印效果
再当前背景之后添加一个水印效果,肯定要使用FrameLayout布局。首先贴下xml布局:

<FrameLayout        android:layout_width="match_parent"        android:layout_height="wrap_content">         <LinearLayout            android:id="@+id/content"            android:layout_width="match_parent"            android:layout_height="match_parent"             android:text="这是你的主体部分"            android:orientation="vertical"/>             </LinearLayout><LinearLayout            android:id="@+id/llrotateTextView"            android:layout_width="match_parent"            android:layout_height="wrap_content">            <com.android.view.RotateTextView                android:id="@+id/rotateTextView"                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:layout_gravity="center"                android:alpha="0.15"                android:text="meiliangyan"                android:textSize="30sp" />        </LinearLayout>    </FrameLayout>
这里使用封装的RotateTextView,里面可以设置文字倾斜public class RotateTextView extends TextView {public int getmDegrees() {    return mDegrees;}public void setmDegrees(int mDegrees) {    this.mDegrees = mDegrees;    invalidate();}private int mDegrees;public RotateTextView(Context context) {    super(context, null);}public RotateTextView(Context context, AttributeSet attrs) {    super(context, attrs, android.R.attr.textViewStyle);    this.setGravity(Gravity.CENTER);    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.LeanTextView);//这里用的styleable    mDegrees = a.getDimensionPixelSize(R.styleable.LeanTextView_degree, 0);    a.recycle();}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {    super.onMeasure(widthMeasureSpec, heightMeasureSpec);    setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth());}@Overrideprotected void onDraw(Canvas canvas) {    canvas.save();    canvas.translate(getCompoundPaddingLeft(), getExtendedPaddingTop());    canvas.rotate(-mDegrees, this.getWidth() / 2f, this.getHeigh() / 2f);//这里的-mDegrees是倾斜/成这样,去掉-,直接mDegrees是倾斜\ 成这样。    super.onDraw(canvas);    canvas.restore();}

}
styleable文件:

<resources>    <declare-styleable name="LeanTextView">        <attr name="degree" format="dimension" />    </declare-styleable></resources>

下面是在activity中的引用。我直接写在一个方法里面。最好开个线程,要不然再绘制容易冲突

 private void water() {        new Thread() {            @Override            public void run() {                try {                    sleep(100);                    runOnUiThread(new Runnable() {                        @Override                        public void run() {                             rotateTextView.setText("水印内容");                    rotateTextView.setTypeface(Typeface.defaultFromStyle(Typeface.BOL  D));                int ww = content.getWidth();//主体内容的宽                int hh = content.getHeight();//主体内容的高        int drgree = (int)Math.toDegrees(Math.atan((double) hh / (double) ww));//倾斜的角度,根据当前内容的长宽,倾斜度数不一样        ViewGroup.LayoutParams p=llrotateTextView.getLayoutParams();                            p.height = hh;                            llrotateTextView.setLayoutParams(p);                            rotateTextView.setmDegrees(drgree);                        }                    });                } catch (Exception e) {                    e.printStackTrace();                }            }        }.start();    }

第一次写博客,写的不好多多见谅,有什么问题也欢迎留言,共同讨论,我是个努力进步的安卓小兵。。。。

原创粉丝点击