当前位置:网站首页>seekbar 自定义图片上下左右显示不全 / bitmapToDrawable / bitmapToDrawable互转 / paddingStart/paddingEnd /thumbOffset
seekbar 自定义图片上下左右显示不全 / bitmapToDrawable / bitmapToDrawable互转 / paddingStart/paddingEnd /thumbOffset
2022-06-29 03:24:00 【Mars-xq】
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, "onCreate:轨道在view中下边距=============> " + paddingBottom);//0
// mSeekBar.setPadding(0, 0, 0, 0);
// mSeekBar.setThumbOffset(0);
}
getSystemVersions();
}
private void getSystemVersions() {
// 系统API版本号-数字格式,例如:30,亦即表示Android API level 30
int version = Build.VERSION.SDK_INT;
// 系统版本号,例如:11,亦即表示Android 11
String strRelease = Build.VERSION.RELEASE;
Log.e(TAG, "androidApiVersion===" + version +
",androidVersion===" + strRelease);
}
}
效果图:

边栏推荐
- Gartner's "voice of customers" has the highest score, and the user experience has become a major breakthrough for China's database
- 【云原生】这么火,你不来了解下?
- Equal wealth
- 逆序对对数计算,顺序对对数计算——归并排序
- 19.03 vessel description and simple application examples continued
- Gcc compiler package
- 【雲原生】這麼火,你不來了解下?
- Double click events and click events
- How to skip time when closing a socket connection_ Wait status of wait
- Certification training | streamnational certification training phase 2
猜你喜欢

Tupu software intelligent energy integrated management and control platform
![[together with Shangshui Shuo series] day 6-strong liver academic paper! The most detailed explanation!](/img/70/595a94ba19d29a56a4f0bb5964a199.png)
[together with Shangshui Shuo series] day 6-strong liver academic paper! The most detailed explanation!

In the name of love, fresh e-commerce companies rush to sell flowers on Valentine's Day

Yyds dry inventory difference between bazel and gradle tools

2022-2028 global CAE engineering service industry research and trend analysis report

vim配置与使用

Problem - ADB shellerror: insufficient permissions for device: verify udev rules
![[linear algebra] 1.2 total permutation and commutation](/img/04/18fc358c6c426e10c8598bcee9cd43.png)
[linear algebra] 1.2 total permutation and commutation

2D人体姿态估计 - DeepPose

LeetCode 每日一题——324. 摆动排序 II
随机推荐
Démarrer le test - test d'intégration
Tu ne peux pas comprendre le feu?
For safe login of wechat applet, the openid returned by wechat must be verified first to ensure the uniqueness of information.
2022-2028 global pneumatic test probe industry survey and trend analysis report
Yyds dry inventory difference between bazel and gradle tools
想当设备管理师?满足这三个报考条件就可以
Sequence traversal of binary tree ii[one of sequence traversal methods - > recursive traversal + level]
Grafana入门教程
Basic MySQL database operations
设备监理师证书含金量怎样?值得考吗?
Problem - ADB shellerror: insufficient permissions for device: verify udev rules
Get error: Unsupported fork ordering: eip150block not enabled, but eip155block enabled at 0
SVN常用的十个命令
Etcd tutorial - Chapter 7 etcd transaction API
gcc编译器包
FPGA (VIII) RTL code IV (basic circuit design 1)
Laravel v. about laravel using the pagoda panel to connect to the cloud database (MySQL)
Leetcode daily question - 324 Swing sort II
Web APIs high order functions - dark horse programmers
Installation and deployment of sw-x framework