自定义View的简单案例(2)

上一篇讲了自定义View的基本用法,这一篇就用来熟悉自定义view以及拓展下一些小效果

步骤和上篇一样,也不啰嗦了直接上代码

首先定义一个类继承View ,此处不同的是需要用到Point 画笔类,所以我们需要先在两个构造方法中首先初始化画笔

package demo.liuchen.com.android27_customview.MyTextViewCustom;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Toast;

import demo.liuchen.com.android27_customview.MainActivity;
import demo.liuchen.com.android27_customview.R;

/**
 * Created by ${LC} on 2016/11/22.
 * /**
 * 步骤:1 创建一个class继承view
 *      2 创建两个构造方法
 *      3 在两个构造方法中初始化画笔
 *      4 再两个参数的构造方法中重写需要重写的属性
 */


public class MyTextView extends View{
    //声明画笔
    private Paint paint;
    private int backColor;
    private String Textcontent;
    private float textScaleX;
    public MyTextView(Context context) {
        super(context);
        initPaint();
    }

    public MyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initPaint();

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyTextView);
        //获去字体大小 并规定默认值
        float textSize = typedArray.getDimension(R.styleable.MyTextView_myview_textSize,0);
        //获去字体颜色 并规定默认值
        int textColor = typedArray.getColor(R.styleable.MyTextView_myview_textColor, Color.WHITE);
        //获取背景颜色,并规定默认值
        backColor = typedArray.getColor(R.styleable.MyTextView_myview_backColor,Color.BLACK);
        //获取文本内容
        Textcontent = typedArray.getString(R.styleable.MyTextView_myview_textContent);
        //获取文本x轴拉伸
        textScaleX   = typedArray.getDimension(R.styleable.MyTextView_myview_textScalex,0);
        //回收typeArray
        typedArray.recycle();
        //设置字体颜色
        paint.setColor(textColor);
        //设置字体大小
        paint.setTextSize(textSize);
        //设置间隔
        paint.setTextScaleX(textScaleX);

    }



    //初始化画笔第一步
    public void initPaint(){
        paint = new Paint();
        //去锯齿,达到圆滑的效果
        paint.setAntiAlias(true);
    }

    //Canvas:画布
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //设置背景颜色
        canvas.drawColor(backColor);
        /**
         *  设置字体内容
         *  @param String :字体内容
         *  @param float :x-coordenate:x的偏移量
         *  @param float :y-coordenate:y的偏移量
         *  @param paint :画笔
         */

        canvas.drawText(Textcontent,10,100,paint);
    }
}

然后在attrs文件中写重写需要重写的属性

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!--declare-styleable 头标签用于规定某个控件的属性的集合
    name:相当于id 用户在java代码中可通过R.styleable.mycustomSimpleView找到
    attr:属性重写 name:再布局文件中重写的属性时用到的属性名
    -->
 <declare-styleable name="MyTextView">
        <!--dimension尺寸-->
        <attr name="myview_textSize" format="dimension"/>
        <attr name="myview_textColor" format="color"/>
        <attr name="myview_backColor" format="color"/>
        <attr name="myview_textContent" format="string"/>
        <attr name="myview_textScalex" format="dimension"/>
    </declare-styleable>
</resources>



然后我们在Acitivity的布局文件使用就行了

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:myapp="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_my_text_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="demo.liuchen.com.android27_customview.MyTextViewCustom.MyTextViewActivity">


    <demo.liuchen.com.android27_customview.MyTextViewCustom.MyTextView
        android:id="@+id/mytextview1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        myapp:myview_textSize="18sp"
        myapp:myview_backColor="@color/colorAccent"
        myapp:myview_textColor="#38d261"
        myapp:myview_textContent="how are you doing "
        myapp:myview_textScalex="1dp"/>

</RelativeLayout>
同样需要加上这一行才能使用自定义view的属性
 xmlns:myapp="http://schemas.android.com/apk/res-auto"

运行就能出现彩色字体的带自定义的背景的颜色


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值