Android View implementation scheme of regional click-1. Based on the coordinate range

Source: Internet
Author: User

Android View implementation scheme of regional click-1. Based on the coordinate range
Android View implementation scheme of regional click-1. Based on the coordinate range

Introduction

Well, I haven't updated my blog posts about Android for a long time. Some friends often ask me in the group or talk to me privately:
Reader: "When is Android advanced? What is Git? Data Structure? Focus on Android advanced ..." And so on,
This shows that I am helpless:

Me:Reader:Me: Okay, blame me...

Well, it's not that you don't want to write it. The main reason is that you have more things, the more things you come into contact with, and the more things you learn,
In addition to finishing the company project, there are some other things to learn. I just finished the radio project some time ago.
Launchar should be used with another colleague as a module. OpenGL should be used in the module.
It's a stranger, and I have to spend time learning it. In this way, I also want to quietly think about something and then write something,
We have to go back to reality. For the sake of life, right! However, I decided to write some projects for you from time to time.
For example, this section describes the implementation scheme of the View area click, which is displayed by another colleague.
You can click different parts of a pig to execute different animations.
View. The solution provided in this section is to determine the click area based on different coordinate ranges.
Start content in this section!

Material Preparation

All right, open the drawing that comes with win and draw a 400*400 image:

Code Implementation

Here, due to the time relationship, you can directly write a View that inherits the ImageView, and then directly set the image background through the attribute.
The key part is in onTouchEvent! Click the coordinates of the area to determine, and then directly call the Activity
Define the display Toast method. This is just a convenient demonstration. In actual development, it is not recommended to expose the method directly!

RegionCoordView. java

/** * Created by coder-pig on 2016/4/12. */public class RegionCoordView extends ImageView {    private Context 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) {            float x = event.getX();            float y = event.getY();            int area = 0;            if (x >= 0 && x < 200 && y > 0 && y < 200) {                area = 1;            } else if (x > 200 && x < 400 && y > 0 && y < 200) {                area = 2;            } else if (x > 0 && x < 200 && y > 200 && y < 400) {                area = 3;            } else if (x > 200 && x < 400 && y > 200 && y < 400) {                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 (int area) {Toast. makeText (MainActivity. this, "you have clicked the" + area + "Block area! ", Toast. LENGTH_SHORT). show ();}}

Activity_main.xml:


  
      
   
  
Running summary:

Okay, the code is very simple. The trick is simply to get the x and y coordinates of the touch point of the TouchEvent,
It is very simple to make judgments. Of course, this scheme applies to fixed general rule graphics, such as the above
Square, triangle, circle, or arc shape, but the more complex the image is, the more difficult it is to judge the region.
Which of the following is the case for you to determine the click area? Different colors indicate the click areas of different responses.


The solution described in this section is obviously hard to play... Okay. Next we will provide you with another solution,
Determine the click area based on the pixel~
PS: Because of the AS, the Code posted here has never been run, and the theory is feasible. Actually, I don't know...
Complete code and run. paste it to your company tomorrow! I wanted to go to bed before.
Great Wall broadband, various garbage, turning over the wall, and Cheng Xiang. As a result, my next gradle will take over an hour... Then,
I really don't know what to say...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.