Imageview and ontouchlistener implementation, click to view image details

Source: Internet
Author: User
Tags gety

This is an example from the crazy android handout. It is to use the listener to get the coordinates of the finger, and then draw an image based on the coordinates. (There are some problems with the rendering method here, so let's take a look .)

 

First, the layout file (two imageviews)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <TextView        android:id="@+id/textView01_id"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center_horizontal"        android:text="Large Text"/>    <ImageView        android:id="@+id/imageView01_id"        android:layout_width="match_parent"        android:layout_height="300dp"        android:src="@drawable/kale"         android:scaleType="fitXY"/>        <ImageView        android:id="@+id/imageView02_id"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:src="@drawable/kale"         android:scaleType="fitXY"/>        </LinearLayout>

Mainactivity. Java

Package COM. kale. imageview02; import android. app. activity; import android. graphics. bitmap; import android. graphics. drawable. bitmapdrawable; import android. OS. bundle; import android. view. motionevent; import android. view. view; import android. view. view. ontouchlistener; import android. widget. imageview; import android. widget. textview; public class mainactivity extends activity {imageview iv01, iv02; textview TV; @ override protected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); viewinit ();} public void viewinit () {iv01 = (imageview) findviewbyid (R. id. imageview01_id); iv02 = (imageview) findviewbyid (R. id. imageview02_id); TV = (textview) findviewbyid (R. id. textview01_id); iv01.setontouchlistener (New ontouchlistener () {@ override public Boolean ontouch (view arg0, motionevent event) {If (event. getaction () = motionevent. action_move) {TV. settext ("x cursor =" + event. getx () + "y cursor =" + event. gety ();} If (event. getaction () = motionevent. action_down) {// obtain bitmapdrawable object bitmapdrawable = (bitmapdrawable) iv01.getdrawable (); // obtain Bitmap bitmap = bitmapdrawable. getbitmap (); // sets the bitmap ratio to double scale = bitmap. getwidth ()/320.0; // defines the starting position of the vertex int x = (INT) (event. getx () * scale); int y = (INT) (event. gety () * scale); If (x + 120> bitmap. getwidth () {x = bitmap. getwidth ()-120;} If (Y + 120> bitmap. getheight () {Y = bitmap. getheight ()-120;} // displays the specified region of the video clip, iv02.setimagebitmap (bitmap. createbitmap (bitmap, X, Y, 120,120);} // If the parameter is set to false, return true will not be displayed for moving the animation ;}});}}

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.