我想创建自定义视图以显示5种不同的颜色作为附加图像:
有谁知道如何实现上面的视图?
谢谢.
解决方法:
您可以通过将自定义Drawable设置为使用自定义Seekbar来完成此 *** 作.
请参阅this示例.
ProgressItem.java
public class ProgressItem { public int color; public float progressItemPercentage;}
CustomSeekbar.java
int progressbarWIDth = getWIDth();int progressbarHeight = getHeight();int thumboffset = getThumbOffset();int lastProgressX = 0;int progressItemWIDth, progressItemRight;for (int i = 0; i < mProgressItemsList.size(); i++) { ProgressItem progressItem = mProgressItemsList.get(i); Paint progresspaint = new Paint(); progresspaint.setcolor(getResources().getcolor(progressItem.color)); progressItemWIDth = (int) (progressItem.progressItemPercentage * progressbarWIDth / 100); progressItemRight = lastProgressX + progressItemWIDth; // for last item give right of the progress item to wIDth of the // progress bar if (i == mProgressItemsList.size() - 1 && progressItemRight != progressbarWIDth) { progressItemRight = progressbarWIDth; } Rect progressRect = new Rect(); progressRect.set(lastProgressX, thumboffset / 2, progressItemRight, progressbarHeight - thumboffset / 2); canvas.drawRect(progressRect, progresspaint); lastProgressX = progressItemRight;}super.onDraw(canvas);}
activity_main.xml中
<yourpackagename.CustomSeekbar androID:ID="@+ID/customSeekbar" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:max="100" androID:progress="0" androID:progressDrawable="@androID:color/transparent" androID:thumb="@drawable/seek_thumb_normal" androID:thumbOffset="12dp" />
MainActivity.java
private CustomSeekbar seekbar;private float totalSpan = 1500;private float redSpan = 200;private float blueSpan = 300;private float greenSpan = 400;private float yellowSpan = 150;private float darkGreySpan;private ArrayList<ProgressItem> progressItemList;private ProgressItem mProgressItem;@OverrIDeprotected voID onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentVIEw(R.layout.activity_main);seekbar = ((CustomSeekbar) findVIEwByID(R.ID.customSeekbar));initDataToSeekbar();}private voID initDataToSeekbar() {progressItemList = new ArrayList<ProgressItem>();// red spanmProgressItem = new ProgressItem();mProgressItem.progressItemPercentage = ((redSpan / totalSpan) * 100);mProgressItem.color = R.color.red;progressItemList.add(mProgressItem);// blue spanmProgressItem = new ProgressItem();mProgressItem.progressItemPercentage = (blueSpan / totalSpan) * 100;mProgressItem.color = R.color.blue;progressItemList.add(mProgressItem);// green spanmProgressItem = new ProgressItem();mProgressItem.progressItemPercentage = (greenSpan / totalSpan) * 100;mProgressItem.color = R.color.green;progressItemList.add(mProgressItem);// yellow spanmProgressItem = new ProgressItem();mProgressItem.progressItemPercentage = (yellowSpan / totalSpan) * 100;mProgressItem.color = R.color.yellow;progressItemList.add(mProgressItem);// greyspanmProgressItem = new ProgressItem();mProgressItem.progressItemPercentage = (darkGreySpan / totalSpan) * 100;mProgressItem.color = R.color.grey;progressItemList.add(mProgressItem);seekbar.initData(progressItemList);seekbar.invalIDate();}
总结 以上是内存溢出为你收集整理的android – 创建5种不同颜色的自定义栏全部内容,希望文章能够帮你解决android – 创建5种不同颜色的自定义栏所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)