我想设置一个如下所示的android progressbar:
我该如何实现?
更新-解决方案:
最后,我能够通过使用九补丁的imageView并更改此imageView相对于其容器的宽度来获得所需的结果。该容器反映进度条的最大(100%)大小。
我已经尝试过的:
使用Android的偏斜方法
progressBar.matrix.setSkew(1f, 0.5f)
没有任何反应,进度条看起来仍然像标准进度条。我也尝试过
progressBar.matrix.postSkew(1f, 0.5f),
试图对setSkew和postSkew使用其他值,并试图
progressbar.invalidate()
设置偏斜后。
使用自定义progressBar.progressDrawable
我还考虑过使用自定义的progressDrawable,但不知道如何实现所需的外观。
原因:进度栏的背景必须是透明的,因此我不能只考虑重叠的矩形,例如here。
是否有一种方法可以使上述方法之一起作用,或者有另一种方法可以达到预期的结果?
预期结果:
<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLmliYi5jby81OUdDekRXL3NrZXdlZC1wcm9ncmVzc2Jhci5wbmcifQ==” alt =“倾斜进度栏”>
实际结果:
<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLmliYi5jby9mR1BSMHhDL3Byb2dyZXNzYmFyLnBuZyJ9” alt =“常规进度栏”>
请尝试此解决方案:
skewed_background.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle"
android:layout_width="match_parent"
android:layout_height="match_parent">
<solid android:color="#3F8EEB" />
</shape>
</item>
<item>
<rotate
android:fromDegrees="120"
android:pivotX="85%"
android:pivotY="98%"
>
<shape android:shape="rectangle"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<solid android:color="#000000" />
</shape>
</rotate>
</item>
<item>
<rotate
android:fromDegrees="120"
android:pivotX="15%"
android:pivotY="%"
>
<shape android:shape="rectangle"
android:layout_width="30dp"
android:layout_height="match_parent">
<solid android:color="#000000" />
</shape>
</rotate>
</item>
</layer-list>
sample_layout.xml
<?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:padding="16dp"
android:orientation="vertical"
>
<View
android:layout_width="150dp"
android:layout_height="50dp"
android:background="@drawable/hfjh" />
</LinearLayout>
结果: