当前位置:网站首页>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);
}
}
效果图:

边栏推荐
- Synchronous movement state of Jerry's watch [chapter]
- Solve the problem that the cursor flashes after clicking a point when measuring the distance in Allegro
- Unable to locate program input point [email protected]
- Double click events and click events
- Allegro's method of setting network flying line and network color
- Sequence traversal of binary tree ii[one of sequence traversal methods - > recursive traversal + level]
- 初探元宇宙存储,数据存储市场下一个爆点?
- Grafana入门教程
- Jerry's watch obtains alarm mode settings [chapter]
- 【线程通信】
猜你喜欢

For safe login of wechat applet, the openid returned by wechat must be verified first to ensure the uniqueness of information.
![恢复二叉搜索树[根据题意模拟->发现问题->分析问题->见招拆招]](/img/06/cc3c512e69455416fe81943bebacca.png)
恢复二叉搜索树[根据题意模拟->发现问题->分析问题->见招拆招]
![[linear algebra] 1.2 total permutation and commutation](/img/04/18fc358c6c426e10c8598bcee9cd43.png)
[linear algebra] 1.2 total permutation and commutation

Nvisual helps integrators transform
![[leetcode daily question] number of schemes to reconstruct a tree](/img/82/2ed8c9747f9fa36fde4f18cf8966be.jpg)
[leetcode daily question] number of schemes to reconstruct a tree

Web GIS 航拍实现的智慧园区数字孪生应用

Problème - Ajouter shellerror: permissions d'instrumentation pour le périphérique: vérifier les règles udev.

逆序对对数计算,顺序对对数计算——归并排序

leetcode:560. 和为 K 的子数组
![Jerry's watch pause [chapter]](/img/57/487ca3dd40e5a7bafdbbe4a7c331e6.jpg)
Jerry's watch pause [chapter]
随机推荐
Is the account opening of GF Securities really safe and reliable
What is the gold content of the equipment supervisor certificate? Is it worth it?
Installation and deployment of sw-x framework
Modstart rewrite rule
SSH无密码登陆
Concise words tell about technical people who must master basic IT knowledge and skills. Part 1
Etcd教程 — 第七章 Etcd之事务API
Movement state change of monitoring device of Jerry's watch [chapter]
恢复二叉搜索树[根据题意模拟->发现问题->分析问题->见招拆招]
Get error: Unsupported fork ordering: eip150block not enabled, but eip155block enabled at 0
数据离散化
相同的树[从部分到整体]
FarrowTech的无线传感器采用橙群微电子的NanoBeacon蓝牙信标技术
Laravel, execute PHP artist migrate and report an error alter table `users`add unique `users_ email_ unique`(`email`))
Codeforces Round #771 (Div. 2) ABC
Solve the problem that the cursor flashes after clicking a point when measuring the distance in Allegro
Tu ne peux pas comprendre le feu?
Pat class a a1057 stack
如何理解MySQL的索引?
目前市面上增额终身寿险利率最高的产品是哪个?