seekbar 自定义图片上下左右显示不全 / bitmapToDrawable / bitmapToDrawable互转 / paddingStart/paddingEnd /thumbOffset
Posted Mars-xq
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了seekbar 自定义图片上下左右显示不全 / bitmapToDrawable / bitmapToDrawable互转 / paddingStart/paddingEnd /thumbOffset相关的知识,希望对你有一定的参考价值。
progressDrawable的高度设置
android:maxHeight="20dp"
android:minHeight="20dp"
maxHeight
/minHeight
: 进度条高度
thumboffset
来设置滑块可超出轨道距离
padding
来设置轨道在view中位置
splitTrack
: 滑块是否使用父布局背景色
,true使用,默认false
解决滑块图片显示不全:
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.SeekBar;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@SuppressLint("AppCompatCustomView")
public class ScaleThumbSeekBar extends SeekBar
private static final String TAG = ScaleThumbSeekBar.class.getSimpleName();
public ScaleThumbSeekBar(@NonNull Context context)
this(context, null);
public ScaleThumbSeekBar(@NonNull Context context, @Nullable AttributeSet attrs)
this(context, attrs, -1);
public ScaleThumbSeekBar(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr)
super(context, attrs, defStyleAttr);
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh)
super.onSizeChanged(w, h, oldw, oldh);
//解决滑动图片上下显示不全
Drawable thumb = getThumb();
Bitmap bitmap = drawableToBitmap(thumb);
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, h, h, true);
BitmapDrawable bitmapDrawable = bitmapToDrawable(scaledBitmap, getContext());
setThumb(bitmapDrawable);
//解决滑动图片左右显示不全
setThumbOffset(0);
setPadding(0, 0, 0, 0);
//调用函数缩小图片
public BitmapDrawable getNewDrawable(Context context, int restId, int dstWidth, int dstHeight)
Bitmap decodeResource = BitmapFactory.decodeResource(context.getResources(), restId);
Bitmap scaledBitmap = Bitmap.createScaledBitmap(decodeResource, dstWidth, dstHeight, true);
BitmapDrawable bitmapDrawable = bitmapToDrawable(scaledBitmap, context);
Bitmap bitmap = bitmapDrawable.getBitmap();
if (bitmap.getDensity() == Bitmap.DENSITY_NONE)
bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics());
return bitmapDrawable;
public BitmapDrawable bitmapToDrawable(Bitmap bitmap, Context context)
return new BitmapDrawable(context.getResources(), bitmap);
private Bitmap drawableToBitmap(Drawable drawable)
if (drawable instanceof BitmapDrawable)
Log.e(TAG, "drawableToBitmap: BitmapDrawable ");
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
return bitmapDrawable.getBitmap();
else
Log.e(TAG, "drawableToBitmap: ! BitmapDrawable ");
int width = drawable.getIntrinsicWidth();
int height = drawable.getIntrinsicHeight();
drawable.setBounds(0, 0, width, height);
Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ?
Bitmap.Config.ARGB_8888 :
Bitmap.Config.RGB_565;
Bitmap bitmap = Bitmap.createBitmap(width, height, config);
Canvas canvas = new Canvas(bitmap);
drawable.draw(canvas);
return bitmap;
demo
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
android:gravity="center"
android:orientation="vertical">
<SeekBar
android:id="@+id/seek_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="50dp"
android:background="@android:color/white" />
<!-- thumbOffset :默认按压放大时滑块可超出轨道的距离 -->
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="50dp"
android:layout_marginTop="30dp"
android:background="@android:color/white"
android:thumbOffset="20dp" />
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="50dp"
android:layout_marginTop="10dp"
android:background="@android:color/white"
android:thumbOffset="0dp" />
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="50dp"
android:layout_marginTop="10dp"
android:background="@android:color/white"
android:thumbOffset="-20dp" />
<!-- paddingStart/paddingEnd :轨道的边距 -->
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="50dp"
android:layout_marginTop="30dp"
android:background="@android:color/white"
android:paddingStart="0dp"
android:paddingEnd="0dp" />
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="50dp"
android:layout_marginTop="10dp"
android:background="@android:color/white"
android:paddingStart="15dp"
android:paddingEnd="15dp" />
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="50dp"
android:layout_marginTop="10dp"
android:background="@android:color/white"
android:paddingStart="25dp"
android:paddingEnd="25dp" />
<!-- android:splitTrack : 滑块是否使用父布局背景色,true使用,默认false -->
<com.xq.myapplication.ScaleThumbSeekBar
android:id="@+id/seekBar"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_marginHorizontal="50dp"
android:layout_marginTop="30dp"
android:background="@android:color/white"
android:progressDrawable="@drawable/progress_drawable"
android:thumb="@mipmap/ic_launcher" />
<com.xq.myapplication.ScaleThumbSeekBar
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_marginHorizontal="50dp"
android:layout_marginTop="10dp"
android:background="@android:color/white"
android:progressDrawable="@drawable/progress_drawable"
android:splitTrack="true"
android:thumb="@mipmap/ic_launcher" />
<com.xq.myapplication.ScaleThumbSeekBar
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_marginHorizontal="50dp"
android:layout_marginTop="10dp"
android:background="@android:color/white"
android:progressDrawable="@drawable/progress_drawable"
android:splitTrack="false"
android:thumb="@mipmap/ic_launcher" />
<!-- android:maxHeight/minHeight : 进度条高度 -->
<com.xq.myapplication.ScaleThumbSeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="50dp"
android:layout_marginTop="10dp"
android:background="@android:color/white"
android:maxHeight="20dp"
android:minHeight="20dp"
android:progressDrawable="@drawable/progress_drawable"
android:splitTrack="true"
android:thumb="@mipmap/ic_launcher" />
<com.xq.myapplication.ScaleThumbSeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="50dp"
android:layout_marginTop="10dp"
android:background="@android:color/white"
android:maxHeight="40dp"
android:minHeight="40dp"
android:progressDrawable="@drawable/progress_drawable"
android:splitTrack="false"
android:thumb="@mipmap/ic_launcher" />
</LinearLayout>
drawable/progress_drawable :
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape>
<solid android:color="#ff0000" />
</shape>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<solid android:color="#0000ff" />
</shape>
</clip>
</item>
</layer-list>
activity:
import androidx.appcompat.app.AppCompatActivity;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.widget.SeekBar;
public class MainActivity extends AppCompatActivity
private static final String TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SeekBar seekBar = findViewById(R.id.seek_bar);
int thumbOffset = seekBar.getThumbOffset();
Log.e(TAG, "onCreate:滑块可超出轨道======> " + thumbOffset);//23
int thumbWidth = seekBar.getThumb().getIntrinsicWidth();
Log.e(TAG, "onCreate:滑块宽度======> " + thumbWidth);//47
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1)
int paddingStart = seekBar.getPaddingStart();
int paddingEnd = seekBar.getPaddingEnd();
int paddingTop = seekBar.getPaddingTop();
int paddingBottom = seekBar.getPaddingBottom();
Log.e(TAG, "onCreate:轨道在view中左边距=============> " + paddingStart);//42
Log.e(TAG, "onCreate:轨道在view中右边距=============> " + paddingEnd);//42
Log.e(TAG, "onCreate:轨道在view中上边距=============> " + paddingTop);//0
Log.e(TAG, 以上是关于seekbar 自定义图片上下左右显示不全 / bitmapToDrawable / bitmapToDrawable互转 / paddingStart/paddingEnd /thumbOffset的主要内容,如果未能解决你的问题,请参考以下文章
带文字的seekbar : 自定义progressDrawable/thumb :解决显示不全
Android SeekBar 设置自定义thumb 图片显示异常