Create a custom view in Android || 在 Android 中创建一个自定义 View
来源:互联网 发布:mysql没有可视化界面 编辑:程序博客网 时间:2024/06/17 06:36
Resume
While we are creating an Android Application, it’s very common for us to create a custom View. This passage will lead you to create your own View with a custom canvas class.
Create a View class
The beginning to create a View is to create a View class.
Then we just go to the project package, and create a View class. In this passage, I will name it SceneCanvas.
First of all, we need to create a class named SceneCanvas which extends the View class. Afterward, to create a list that contains elements is a good idea.
public SceneCanvas extends View{ List<Bitmap> bitmaps = new ArrayList<Bitmap>();}
Then we will have to add 2 constructors.
public SceneCanvas(Context context){ super(context); } public SceneCanvas(Context context, AttributeSet attributeSet){ super(context, attributeSet); }
Otherwise, when we inflate this view, our App will raise an
android.view.InflateExceptionjava.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
exception which indicates we didn’t implement a constructor which we should have done.
Then we should create a function named onDraw() which will paint some elements on the screen.
Here’s the code.
@Overrideprotected void onDraw(Canvas canvas){ super.onDraw(canvas); canvas.drawColor(Color.LTGRAY); canvas.drawBitmap(bitmaps.get(0), canvas.getWidth() / 2, 0, null);}
Well, you may notice that we didn’t add any element to the list. So I would suggest you to create a function named init() that init everything you need. Hence, we don’t have to write so many codes in those 2 constructors above. We can just invoke them in those 2 constructors.
private void init() { bitmaps.add(BitmapFactory.decodeResource(getResources(), R.drawable.c2));}
The code below shows the entire class:
List<Bitmap> bitmaps = new ArrayList<Bitmap>(); public SceneCanvas(Context context) { super(context); init(); } public SceneCanvas(Context context, AttributeSet attributeSet) { super(context, attributeSet); init(); } private void init() { bitmaps.add(BitmapFactory.decodeResource(getResources(), R.drawable.c2)); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawColor(Color.LTGRAY); canvas.drawBitmap(bitmaps.get(0), canvas.getWidth() / 2, 0, null); }}
XML
The following jobs would be easy, we have 2 ways to inflate the View that we have created before.
The easiest way is to write in the XML file
<your_package_name.SceneCanvas android:id="@+id/scene_canvas" android:layout_width="match_parent" android:layout_height="match_parent" />
Instantiate in Activity/Fragment
The second way is to instantiate in an Activity or Fragment. Here we will instantiate in a Fragment.
public class MyFragment extends Fragments { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.my_fragmen, container, false);//Inflate a layout. RelativeLayout relativeLayout = (RelativeLayout) view.findViewById(R.id.root_view);//Get the root view of the layout. relativeLayout.addView(new SceneCanvas(getActivity()));//Add out SceneCanvas view to the root view. return view; }}
After inflating a layout, we just find a View in that layout, then add our SceneView as a sub-view to that View.
- Create a custom view in Android || 在 Android 中创建一个自定义 View
- android自定义View_0——Create a custom view class
- 在Android创建自定义View
- Android Custom View (自定义 View)
- Get “android:layout_height” from `AttributeSet` in a custom view constructor
- Android custom view
- Android Custom View NullPointerException
- android custom view
- Android - Custom View
- android : custom view
- Android 中自定义 view
- Android中自定义View
- android中自定义View
- Android中自定义View
- Android中自定义View
- Android中自定义View
- Android中自定义view
- android中自定义View
- 知乎某处XSS导致刷粉-javascript的巧妙利用
- mac下apach配置需要注意的一些点
- EditText之显示密码
- 解压缩工具类
- 利用宏向excel表格中批量插入图片
- Create a custom view in Android || 在 Android 中创建一个自定义 View
- Android初级-界面编程第一篇之LinearLayout,RelativeLayout
- 微软最新账户身份验证漏洞分析-20160407-邮箱跳转后的XSS
- 基于C#反射机制的工厂模式
- 案例3计算平台长度程序
- 死循环问题窥视系统main函数初始化之前操作
- javascript 实现文件下载功能
- 使用restful请求https身份不通过, unable to find valid certification path to requested target,使用java生成签名证书
- Centos7下PHP安装gd库