1. 程式人生 > >學習安卓小碼哥自定義控制元件的筆記(六)

學習安卓小碼哥自定義控制元件的筆記(六)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="ScrollingView">
        <attr name="scrollingBg" format="reference" />
        <attr name="speed" format="float" />
    </declare-styleable>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:wtz="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.example.wtz.scrollingview.ScrollingView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        wtz:scrollingBg="@mipmap/ic_launcher"
        wtz:speed="2.0" />

</RelativeLayout>
package com.example.wtz.scrollingview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

public class ScrollingView extends View {
    private Paint mPaint;
    private float mSpeed;
    private Bitmap mBitmap;

    public ScrollingView(Context context) {
        super(context);
    }

    public ScrollingView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        //獲取一下自定義屬性的值
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ScrollingView);
        mSpeed = typedArray.getFloat(R.styleable.ScrollingView_speed, 1.0f);
        int resourceId = typedArray.getResourceId(R.styleable.ScrollingView_scrollingBg, 0);
        mBitmap = BitmapFactory.decodeResource(getResources(), resourceId);
        typedArray.recycle();
        mPaint = new Paint();
    }

    //首先把大小確定
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //寬度不做修改,高度設定成合理的值
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        setMeasuredDimension(widthMeasureSpec, mBitmap.getHeight());

    }

    float mOffset=0;

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

        //1.填充控制元件剩下的寬度,多繪製幾個bitmap上去
        float left = mOffset;
        while(left<getMeasuredWidth()){
            canvas.drawBitmap(mBitmap, left, 0, mPaint);
            left+=mBitmap.getWidth();
        }

        //2.動起來,讓left這個左邊基座標不停的去變化
        //根據速度來變化,通過速度來生成偏移量,偏移量去影響left

        mOffset-=mSpeed;

        //3.讓控制元件重新繪製
        invalidate();
    }

}

在這裡插入圖片描述
在這裡插入圖片描述