Android View Sub-region click Implementation--1. According to the coordinate range
Introduction
Good, long time no update blog about Android, a small group of friends often ask or private chat me:
reader : "When Android advanced, write git, data structure, focus on Android Advanced ..."
I am very helpless to say this:
me : reader : i : Well, blame me ...
Well, not that they do not want to write, mainly their own things are more, contact the more things, learn more things,
In addition to the completion of the company project, there are some other things to learn, some time ago just finished radio project, now
To do Launchar with another colleague, do one of the modules, the module needs OpenGL, for OpenGL
It's strange, and it takes time to learn, so I want to think quietly about something and write something, but
To return to reality, in order to live, yes! But decided, not regularly to everyone to write some of their own projects encountered
To some of the things, such as this SectionTo-speaking view sub-region Click Implementation, which is seen in another colleague's item
, click on the different parts of a pig, perform a different animation, and this pig is a self-written to display the animation
View, this section provides the scenario is: according to different coordinate range to determine the click Area, OK, say not much, open
Start the content of this section!
Material preparation
OK, open the drawing of win comes in handy, draw a 400 * 400 graph:
Code implementation
Because of the temporal relationship, we write a view that inherits ImageView directly and then set the picture background directly from the property.
The key part in the Ontouchevent section! This determines the area where the coordinates are clicked, and then calls the activity directly.
Definition of the method of displaying toast, here is just a handy demonstration, the actual development is not recommended to directly expose the method!
Regioncoordview.java
/** * Created by Coder-pig on 2016/4/12. * * Public class regioncoordview extends ImageView { PrivateContext Mcontext; Public Regioncoordview(Context context) { This(Context,NULL); } Public Regioncoordview(context context, AttributeSet attrs) {Super(context, attrs); Mcontext = context; Init (); }Private void Init() { }@Override protected void OnDraw(Canvas canvas) {Super. OnDraw (canvas); }@Override Public Boolean ontouchevent(Motionevent event) {if(event.getaction () = = motionevent.action_button_press) {floatx = Event.getx ();floaty = event.gety ();intArea =0;if(x >=0&& x < $&& y >0&& y < $) {area =1; }Else if(X > $&& x < -&& y >0&& y < $) {area =2; }Else if(X >0&& x < $&& y > $&& y < -) {area =3; }Else if(X > $&& x < -&& y > $&& y < -) {area =4; } ((mainactivity) mcontext). Showclickarea (area); }return Super. Ontouchevent (event); }}
Mainactivity.java
Public class mainactivity extends appcompatactivity { @Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main); } Public void Showclickarea(intArea) {Toast.maketext (mainactivity. This,"You clicked on the section"+ Area +"Block area!" ", Toast.length_short). Show (); }}
activity_main.xml:
<?xml version= "1.0" encoding= "Utf-8"?><relativelayoutxmlns:android="Http://schemas.android.com/apk/res/android" android:layout_width="Match_parent"android:layout_height="Match_parent" > <com.coderpig.app.regionclickdemo.RegionCoordViewandroid:layout_width="400DP" android:layout_height="400DP"android:background="@mipmap/bg_region"/ > </relativelayout>
To run a summary:
Well, the code is pretty simple, but the trick is to get the x, y coordinates of the touchevent touch Point,
And then make a judgment, very simple, of course, this scheme is suitable for fixing ordinary regular graphs, such as the above
Squares, triangles, circles, or arcs, but the more complex the image, the more troublesome the area is to judge, if
Is this the way you want to judge the area of the click? Different colors represent different response areas of the click.
The scenario described in this section is obviously too much to play ... Okay, the next section offers you another solution,
depending on the pixel point to determine the click Area ~
PS: Because of the reason, the code here is not running, the theory is feasible ha, actually do not know ...
Complete code and run, tomorrow to the company in the post! I was supposed to go to bed before 12 o'clock.
The Great Wall broadband, all kinds of rubbish, turn over the wall Ka Chengxiang, the result next gradle under me 1 hour ... And then it's not finished,
I really don't know what to say.
Android View Sub-region click Implementation--1. According to the coordinate range