android开发步步为营之104:文字加阴影效果和描边效果的实现

          有时候为了文字的美观,我们需要给文字加阴影,或者加边框,这个效果怎么实现呢?最近项目里也用到了,这里总结一下。

         一、文字加阴影效果

         1、通过xml实现

  <TextView
        android:id="@+id/txt_shadom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:shadowColor="#ff0000ff"
        android:shadowDx="2"
        android:shadowDy="2"
        android:shadowRadius="1"
        android:text="Shadom"
        android:textColor="#000000"
        android:textSize="30sp" />

          2、通过代码实现

    //设置阴影效果
        TextView txtShadom =(TextView)findViewById(R.id.txt_shadom);
        txtShadom.setShadowLayer(2,1,1, Color.parseColor("#ff0000"));

      参考文档:关于Paint


setShadowLayer

Added in  API level 1
void setShadowLayer (float radius, 
                float dx, 
                float dy, 
                int shadowColor)

This draws a shadow layer below the main layer, with the specified offset and color, and blur radius. If radius is 0, then the shadow layer is removed.

Can be used to create a blurred shadow underneath text. Support for use with other drawing operations is constrained to the software rendering pipeline.

The alpha of the shadow will be the paint's alpha if the shadow color is opaque, or the alpha from the shadow color if not.



          二、文字描边效果

          最近,我们有个需求,要给impact字体加外边框,一开始我的的思路是,这应该是impact的另外一种字体,但是网上都没有找到,后来,通过查找资料,目前加外边框的效果的一种实现方法是通过上下两层的textview通过的不同的字体颜色的叠加来实现的,这里,我也是这么实现的。

         1、通过TextView叠加来展示

   <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/txt_typeface"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/hello_world"
            android:textColor="#000000"
            android:textSize="30sp" />

        <TextView
            android:id="@+id/txt_typeface1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/hello_world"
            android:textColor="#ffffff"
            android:textSize="30sp" />
    </FrameLayout>

    //描边效果
        TextView textView =(TextView)findViewById(R.id.txt_typeface);
        try {
            Typeface typeFace = Typeface.createFromAsset(getAssets(), "fonts/impact.ttf");
            textView.setTypeface(typeFace);
        }catch (Exception e)
        {
            CommonUtil.printStackTrace(e);
        }

        TextPaint tp1 = textView.getPaint();
        tp1.setStrokeWidth(5);
        tp1.setStyle(Paint.Style.FILL_AND_STROKE);
//        tp1.setFakeBoldText(true);
        tp1.setAntiAlias(true);

        TextView textView1 =(TextView)findViewById(R.id.txt_typeface1);
        try {
            Typeface typeFace = Typeface.createFromAsset(getAssets(), "fonts/impact.ttf");
            textView1.setTypeface(typeFace);
        }catch (Exception e)
        {
            CommonUtil.printStackTrace(e);
        }

            2、将描边效果文字绘制到图片中

public static Bitmap createMeme(Context context, Bitmap bitmapOriginal, int textColor, ArrayList<MemeTextNew> memeTexts) {
        if (memeTexts.size() == 0) {
            return bitmapOriginal;
        }

        int width = bitmapOriginal.getWidth(), height = bitmapOriginal.getHeight();
        System.out.println("宽" + width + "高" + height);
        Bitmap bmpMeme = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); //建立一个空的BItMap
        Canvas canvas = new Canvas(bmpMeme);//初始化画布绘制的图像到icon上

        Paint photoPaint = new Paint(); //建立画笔
        photoPaint.setDither(true); //获取跟清晰的图像采样
        photoPaint.setFilterBitmap(true);//过滤一些

        Rect src = new Rect(0, 0, width, height);//创建一个指定的新矩形的坐标
        Rect dst = new Rect(0, 0, width, height);//创建一个指定的新矩形的坐标
        canvas.drawBitmap(bitmapOriginal, src, dst, photoPaint);//将photo 缩放或则扩大到 dst使用的填充区photoPaint

        for (MemeTextNew memeText : memeTexts) {
            TextPaint textPaint = new TextPaint();
            textPaint.setColor(textColor);
            textPaint.setAntiAlias(true);
            /**
            if (memeText.getContent().length() > 60) {
                textPaint.setTextSize(CommonUtil.sp2px(context, 14));
                //textPaint.setTextSize(16);
            } else {
                textPaint.setTextSize(CommonUtil.sp2px(context, 20));
                //textPaint.setTextSize(20);

            }
             **/
            //20160519改成自动获取
            textPaint.setTextSize(memeText.getTextSize());

            textPaint.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/impact.ttf"));
            if (StringUtil.isNotEmpty(memeText.getContent())) {
                //设置边框效果的背景文字,颜色为黑色
                TextPaint strokeTextPaint = new TextPaint();
                strokeTextPaint.setColor(Color.BLACK);
                strokeTextPaint.setStrokeWidth(5);
                strokeTextPaint.setStyle(Paint.Style.FILL_AND_STROKE);
                strokeTextPaint.setAntiAlias(textPaint.isAntiAlias());
                strokeTextPaint.setTextSize(textPaint.getTextSize());
                strokeTextPaint.setTypeface(textPaint.getTypeface());
                StaticLayout strokeLayout = new StaticLayout(memeText.getContent(), strokeTextPaint, (int) memeText.getWidth(), Layout.Alignment.ALIGN_CENTER, 1.0F, 0.0F, true);
                canvas.translate(memeText.getX(), memeText.getY());
                strokeLayout.draw(canvas);

                //绘制前景色文字
                StaticLayout layout = new StaticLayout(memeText.getContent(), textPaint, (int) memeText.getWidth(), Layout.Alignment.ALIGN_CENTER, 1.0F, 0.0F, true);
//                canvas.translate(memeText.getX(), memeText.getY());
                layout.draw(canvas);

                //20160516绘制完成回到原点
//                canvas.translate(-memeText.getX(), -memeText.getY());


            }
        }

        canvas.save(Canvas.ALL_SAVE_FLAG);
        canvas.restore();
        return bmpMeme;
    }





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值