Custom Controls --- inherit the ViewGroup class method (step 1 of step 1 Effect ---- drag the image left and right), and viewgroup Step 2

Source: Internet
Author: User

Custom Controls --- inherit the ViewGroup class method (step 1 of step 1 Effect ---- drag the image left and right), and viewgroup Step 2

----------------------- The following effect is just the beginning of the overall effect-(currently, you can only drag the image left and right) --- continue to update the blog -------------------------


Configuration File

Activity_main.xml (not explained, there is only one custom control)

<RelativeLayout 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" >    <com.example.mscrollview.mScrollView        android:id="@+id/msv"        android:layout_width="match_parent"        android:layout_height="match_parent" /></RelativeLayout>

MainActivity. java, is a combination, and something needs to be added, OK? OK !)

Package com. example. mscrollview; import android. app. activity; import android. OS. bundle; import android. widget. imageView; public class MainActivity extends Activity {// 1. Image resource private int [] ids = {R. drawable. a1, R. drawable. a2, R. drawable. a3, R. drawable. a3, R. drawable. a5, R. drawable. a6}; // 2. Define the custom control private mScrollView msv; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // create a custom control object msv = (mScrollView) findViewById (R. id. msv); // traverse 6 images cyclically, add background images to image objects in sequence, and then add them to the custom msv Control for (int I = 0; I <ids. length; I ++) {ImageView iv = new ImageView (this); iv. setBackgroundResource (ids [I]); // 3. Add the child page msv. addView (iv );}}}
MScrollView. java

Package com. example. mscrollview; import android. content. context; import android. util. attributeSet; import android. view. gestureDetector; import android. view. motionEvent; import android. view. view; import android. view. viewGroup; public class mScrollView extends ViewGroup {// define gesture reader private GestureDetector gestureDetector; // 1. constructor public mScrollView (Context context, AttributeSet attrs) {super (context, attrs ); initView (context);}/** 2. Obtain the location and size of the Child page */@ Overrideprotected void onLayout (boolean changed, int l, int t, int r, int B) {for (int I = 0; I <getChildCount (); I ++) {// obtain the child View childView = getChildAt (I) based on the id ); // layout the childView. layout (I * getWidth (), 0, getWidth () + getWidth () * I, getHeight ());}} /** initialize view */private void initView (Context context) {// Gesture Recognition instantiation gestureDetector = new GestureDetector (context, new GestureDetector. simpleOnGestureListener () {// call this method by dragging your finger. e1 indicates pressing, e2 indicates leaving the event @ Overridepublic boolean onScroll (MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {// The distance between the x axis and the y axis to scrollBy (int) distanceX, 0); // return true after the event is processed; // scrollTo (x, y) });}/*** receives the event onScroll */@ Overridepublic boolean onTouchEvent (MotionEvent event) {super. onTouchEvent (event); // executes the parent class method, and the event is not blocked. // receives the gestureDetector event. onTouchEvent (event); return true ;}}



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.