How to create a custom view in android

南笙酒味 提交于 2020-01-05 10:29:52

问题


I want to learn, how to create custom view. I have gone through multiple tutorial and didn't get what i want(I mean concept).

What i have learned

I am sharing my concept,if i am wrong please correct me.

If you want to make a minor change to the standard view then inherit itself(e.g extends seekbar) and customize it.

But if you want to make difficult or multifunction view, We should inherit View Class

When we inherit a View.

Two methods are neccessary

  1. onDraw(Canvas canvas) // it call each time it and also can be call using invalidate.
  2. onMeasure // it measure the actual width and size and set our viewit accordingly

Whatever you want to draw on View can be done using canvas;(Am i right).

To define additional attributes create an attrs.xml file in your res/values folder. and add <declare-styleable name="YourCustomView"> and add attributes to it.

get these attribute in the constructor of the View using TypedArray call recyle method.

My Confusions

  1. can we draw something without using Canvas
  2. can we draw ImageView without converting it to bitmap.
  3. I simply want to create a custom imageView which can be rotated like a dialView but it should rotate half(semi-circle) and also want to draw its progress outside it. using canvas.drawArc()

What i want to achieve

I want to create my own custom knob, I have searched our google but didn't find what i want see my questions here

First Question

Second Question

Thanks in Advance....


回答1:


1)You can use openGL. Otherwise you need a canvas. The canvas defines where you're drawing to, without it (or the openGL equivalent) we don't know where to draw. You might not want to draw directly to the screen, its common to draw to bitmaps in many cases.

2)Your question doesn't make a lot of sense. Why would you want to draw an ImageView, other than by letting it draw itself?

3)Easiest way to do that is to use a normal image view and call setRotation to rotate it as needed.



来源:https://stackoverflow.com/questions/27728913/how-to-create-a-custom-view-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!