自定义多图片重叠view

多view重叠

使用FrameLayout来使多个view重叠在一起显示 参考:https://blog.csdn.net/hemeinvyiqiluoben/article/details/50499092

https://blog.csdn.net/u012995888/article/details/80189486

UI开发参考

https://blog.csdn.net/Hystudio_lzu/article/details/78570581

自定义view

https://www.jianshu.com/p/705a6cb6bfee

例子

package com.example.myapplication1;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

public class MyView extends FrameLayout {

    Float mDegree;
    ImageView circle1;
    ImageView line1;
    TextView logText;
    ObjectAnimator circleAnim;

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

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

        circle1 = new ImageView(getContext());
        circle1.setImageResource(R.drawable.ic_data_usage_black_24dp);
        line1 = new ImageView(getContext());
        line1.setImageResource(R.drawable.ic_linear_scale_black_24dp);

        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        this.addView(circle1, params);
        this.addView(line1, params);

        this.setBackgroundColor(Color.RED);


        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(circle1, "rotation", 0f, 360f).setDuration(6000);
        objectAnimator.setRepeatCount(ValueAnimator.INFINITE);
        objectAnimator.setInterpolator(new LinearInterpolator());
        objectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                mDegree = (Float) animation.getAnimatedValue("rotation");
            }
        });
        objectAnimator.start();
        circleAnim = objectAnimator;

        this.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                changeImage(v);
            }
        });
    }

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


//    @Override
//    protected void onLayout(boolean changed, int l, int t, int r, int b) {
//        super.onLayout(changed, l, t, r, b);
//    }

    public void getCureentDegree(View view) {
        logText.setText(mDegree + "");
    }

    public void changeImage(View view) {
        Float cureentDegree = mDegree;
        if (circleAnim != null) {
            circleAnim.end();
            circleAnim = null;
        }
//        circle1.setImageDrawable(getDrawable(R.drawable.ic_gps_off_black_24dp));
        circle1.setRotation(cureentDegree);
    }

}

你可能感兴趣的:(自定义多图片重叠view)