直播小程序源码,自定义支持360度旋转的View
自定义Touch360ImageView的代码如下:
package com.example.myapplication;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.LevelListDrawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
public class Touch360ImageView extends androidx.appcompat.widget.AppCompatImageView {
private float mDownX;
private float mLastX;
private long mTouchTime;
private int mImageLevel;
private int mDrawableCount;
private static final int TOUCH_SPACING = 5; //触摸间距
private static final int TIME_INTERVAL = 200; //触摸时间间隔
public Touch360ImageView(Context context) {
this(context, null);
}
public Touch360ImageView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public Touch360ImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.Touch360ImageView);
int arrayId = typedArray.getResourceId(R.styleable.Touch360ImageView_drawableArray, R.array.image_list);
typedArray.recycle();
TypedArray drawableArray = context.getResources().obtainTypedArray(arrayId);
LevelListDrawable levelListDrawable = new LevelListDrawable();
mDrawableCount = drawableArray.length();
for (int i = 0; i < mDrawableCount; i++) {
levelListDrawable.addLevel(0, i, getResources().getDrawable(drawableArray.getResourceId(i, 0)));
}
setImageDrawable(levelListDrawable);
drawableArray.recycle();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mDownX = event.getRawX();
mLastX = mDownX;
mTouchTime = System.currentTimeMillis();
break;
case MotionEvent.ACTION_MOVE:
int dx = (int) (event.getRawX() - mLastX);
mLastX = event.getRawX();
if (dx > TOUCH_SPACING) {
mImageLevel++;
if (mImageLevel >= mDrawableCount) {
mImageLevel = 0;
}
} else if (dx < -TOUCH_SPACING) {
mImageLevel--;
if (mImageLevel < 0) {
mImageLevel = mDrawableCount - 1;
}
}
setImageLevel(mImageLevel);
break;
case MotionEvent.ACTION_UP:
if (System.currentTimeMillis() - mTouchTime < TIME_INTERVAL && Math.abs(mDownX - event.getRawX()) < TOUCH_SPACING) {
performClick();
return false;
}
break;
}
return true;
}
}
可根据效果调整触摸间距和时间间隔以达到更好的效果。支持根据自定义图片数量来刷新图片。
布局文件中的代码
<com.example.myapplication.Touch360ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
app:drawableArray="@array/image_list"/>
以上就是 直播小程序源码,自定义支持360度旋转的View,更多内容欢迎关注之后的文章
标签:自定义,int,private,源码,context,android,mImageLevel,360,Touch360ImageView From: https://www.cnblogs.com/yunbaomengnan/p/17466334.html