我想设置一个如下所示的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。
有没有办法使上述方法之一起作用,或者有另一种方法可以达到预期的效果?
预期结果:
实际结果:
最佳答案
请尝试以下解决方案:
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>
结果: