简体   繁体   English

Android自定义视图onDraw

[英]Android custom view onDraw

I'm trying to make a control that is a composition of another controls. 我正在尝试制作一个由其他控件组成的控件。 I've tried many ways but none seems to work... You ask why do I need that? 我已经尝试了很多方法,但似乎都没有效果……您问我为什么需要它? Becaused 因为

  1. I've faced the impossibility of creating a TextView with different colors of shadow and gradient foreground 我已经面临无法创建具有不同颜色的阴影和渐变前景的TextView的可能性
  2. I want to make a volume text and it's going to be presented by this custom controls 我要制作一个体积文本,它将由此自定义控件呈现

     public class MainMenuItem extends View { private MainMenuItemHelper firstLayerItem; private MainMenuItemHelper secondLayerItem; ... @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); secondLayerItem.draw(canvas); firstLayerItem.draw(canvas); } } public class MainMenuItemHelper extends TextView { private List<DrawCommand> commands; ... @Override protected void onDraw(Canvas canvas) { for (DrawCommand command : commands) { command.draw(canvas, getPaint()); super.onDraw(canvas); } } } 

a control that is a composition of another controls 由其他控件组成的控件

You need at least a Layout like RelativeLayout or LinearLayout as a View can't contain another View. 您至少需要一个像RelativeLayoutLinearLayout这样的布局,因为一个视图不能包含另一个视图。 You need a ViewGroup instead. 您需要一个ViewGroup

I don't know where and how you want to place it but the easiest way is to simply make a xml file containing your controls and style them like you would normally do. 我不知道您想在何处以及如何放置它,但是最简单的方法是简单地制作一个包含控件的xml文件,并像平时一样设置它们的样式。

A shadow color can be defined by android:shadowColor to have a shadow for your text. 阴影颜色可以由android:shadowColor定义以为您的文本添加阴影。

Making the text itself being a gradient might be very tricky. 使文本本身成为渐变可能非常棘手。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM