先看效果图

上一篇自定义view2因为字数的原因没显示在首页,貌似只能在我随笔列表里看,这次是第三篇,为了凑点字数,把关键的地方提出来看一下

1canvas.drawText 的参数是怎么控制 文字的位置的

canvas.drawText(文字, x, y,paint); 

其中x默认应该是文字的左边距,y所对应的水平线是文字的底, 而不是文字的中心点,这里比较重要,显然把我们把想要设置的中心点坐标x,y直接填进去 是不会得到我们想要的效果的

X轴怎么设置文字以 x参数为中心点,主要靠paint设置一个参数

paint.setTextAlign(Paint.Align.CENTER); //这样设置后x坐标点为文字的水平中心

Y轴怎么设置文字以y参数为中心点,主要考计算, 假设高度100的控件,怎么把字写到50的高度上, y这个参数大概约等于 50 + 文字大小 / 4 + 一点点(加不加都行,我上面的效果是没加,文字有点靠上了)

2画一个圆的方法

canvas.drawCircle(x, y, paind);

3直接看整体代码吧,还是在上一篇的基础上做的改动

public class AAAView extends View {

    private Paint paint;
    private Paint bluePaint;
    private Paint whitePaint;
    private Paint greenPaind;
    private Scroller scroller;
    private float moveX;//移动距离
    private float downX;//手指按下的位置
    private int r;//每个字的半径
    private int d;//直径
    private int h;//控件高度
    private int textSize = 25;
    private int location;//手指按在哪个字上
    private int checked = 3;//选中变蓝色
    private String[] week = new String[]{"一", "二", "三", "四", "五", "六", "日"};
    private Context context;


    public AAAView(Context context) {
        super(context);
        init(context);
    }

    public AAAView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public AAAView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {

        paint = new Paint();
        bluePaint = new Paint();
        whitePaint = new Paint();
        greenPaind = new Paint();
        scroller = new Scroller(context);

        paint.setColor(Color.BLACK);
        paint.setAntiAlias(true);
        paint.setTextSize(textSize);
        paint.setTextAlign(Paint.Align.CENTER);

        bluePaint.setColor(Color.BLUE);
        bluePaint.setAntiAlias(true);
        bluePaint.setTextSize(textSize);
        bluePaint.setTextAlign(Paint.Align.CENTER);

        greenPaind.setColor(Color.GREEN);
        greenPaind.setAntiAlias(true);
        greenPaind.setTextSize(textSize);
        greenPaind.setTextAlign(Paint.Align.CENTER);

        whitePaint.setColor(Color.WHITE);
        whitePaint.setAntiAlias(true);
        whitePaint.setTextSize(textSize);
        whitePaint.setTextAlign(Paint.Align.CENTER);

        this.context = context;

    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        r = getWidth() / 14;
        d = 2 * r;
        h = getHeight();

        canvas.drawLine(0, h / 2, getWidth(), h / 2, paint);

        canvas.drawCircle(r + checked * d + moveX, h / 4 * 3, 25, greenPaind);

        for (int i = 0; i < 7; i++) {
            if (i == 5 || i == 6) {
                canvas.drawText(week[i], r + i * d, h / 4 + textSize / 2, bluePaint);

                canvas.drawText(i + "", r + i * d + moveX, h / 4 * 3 + textSize / 4, bluePaint);
            } else {

                canvas.drawText(week[i], r + i * d, h / 4 + textSize / 2, paint);

                canvas.drawText(i + "", r + i * d + moveX, h / 4 * 3 + textSize / 4, paint);
            }
        }

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downX = event.getX();
                location = (int) Math.ceil(downX / d);
                return true;
            case MotionEvent.ACTION_MOVE:
                moveX = (int) (event.getX() - downX);
                invalidate();
                return true;
            case MotionEvent.ACTION_UP://每次抬起手势后,要恢复初始状态
                moveX = 0;
                invalidate();
                if (event.getX() < location * d && event.getX() > (location - 1) * d) {
                    Toast.makeText(context, "点击了第" + location + "个字", Toast.LENGTH_LONG).show();
                    checked = location - 1;
                    invalidate();
                }
                return true;
        }
        return super.onTouchEvent(event);
    }
}

离做成一个日历又近了一步,下一篇应该会写流畅的滑动,现在最后复原的时候是直接复原,太生硬了,再往后就是填上真的日期,然后在完善一些逻辑就差不多搞定了。

内容来源于网络如有侵权请私信删除
你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!