-
Notifications
You must be signed in to change notification settings - Fork 0
自定义view
DawnSpring edited this page Apr 2, 2021
·
2 revisions
package com.qiyi.shortvideo.videocap.utils;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.SeekBar;
import com.qiyi.shortvideo.R;
/**
* Created by LiWeiping on 2019/12/4.
* 水平seeekbar和竖直seekbar
*/
@SuppressLint("AppCompatCustomView")
public class VHSeekBar extends SeekBar {
private int seekBarType;
public enum SeekBarType{
VerticalSeekBarType(0),
HorizontalNormalType(1);
public int value;
SeekBarType(int value) {
this.value = value;
}
}
public VHSeekBar(Context context) {
super(context);
initData(context, null);
}
public VHSeekBar(Context context, AttributeSet attrs) {
super(context, attrs);
initData(context, attrs);
}
public VHSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initData(context, attrs);
}
private void initData(Context context, AttributeSet attrs){
if (attrs != null) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.VHSeekBar);
seekBarType = typedArray.getInt(R.styleable.VHSeekBar_VHSeekBarType, SeekBarType.HorizontalNormalType.value);
typedArray.recycle();
}
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
if(seekBarType == SeekBarType.VerticalSeekBarType.value){
super.onSizeChanged(h, w, oldh, oldw);
}else{
super.onSizeChanged(w, h, oldw, oldh);
}
}
@Override
protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected synchronized void onDraw(Canvas canvas) {
if(seekBarType == SeekBarType.VerticalSeekBarType.value){
canvas.rotate(-90);
canvas.translate(-getHeight(), 0);
}
super.onDraw(canvas);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
if (seekBarType == SeekBarType.VerticalSeekBarType.value) {
if (!isEnabled()) {
return false;
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
setProgress(getMax()
- (int) (getMax() * event.getY() / getHeight()));
onSizeChanged(getWidth(), getHeight(), 0, 0);
break;
case MotionEvent.ACTION_CANCEL:
break;
}
}
return true;
}
}
arrays.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="VHSeekBar">
<attr name="VHSeekBarType" format="enum">
<enum name="VerticalSeekBarType" value="0"/>
<enum name="HorizontalNormalType" value="1"/>
</attr>
</declare-styleable>
</resources>
<com.example.myapplication.VHSeekBar
android:id="@+id/mopi_progress"
android:layout_height="20dp"
android:layout_width="250dp"
android:max="100"
android:maxHeight="3dp"
android:minHeight="3dp"
android:progress="50"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/tv_1"
android:layout_marginTop="80dp"
android:progressDrawable="@drawable/sv_seek_progress_bg"
android:thumb="@drawable/seekbar_thumb_white_15_10"
app:VHSeekBarType="HorizontalNormalType"/>
sv_seek_progress_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape>
<corners android:radius="3dp" />
<solid android:color="#4c616161" />
</shape>
</item>
<item android:id="@android:id/progress">
<clip
android:clipOrientation="horizontal"
android:gravity="left">
<shape>
<corners android:radius="3dp" />
<gradient
android:startColor="#FF0AA6"
android:endColor="#0DFCFF" />
</shape>
</clip>
</item>
</layer-list>
seekbar_thumb_white_15_10.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF"/>
<size android:width="14dp" android:height="14dp"/>
<corners android:radius="7dp"/>
</shape>