[6 basic learning for Android]-common effect 1

Source: Internet
Author: User

 Disclaimer: The book "Secrets of Android Application Development", which records the logs of the book, references the relevant code and summary, and has no commercial use, it is completely a record of self-learning, and many problems will inevitably occur in learning just now. If there are any mistakes, please criticize them a lot.

Continue to learn the book "Secrets of Android Application Development". Here we classify the learning of basic controls. The following two blogs mainly show some common effects.

1. Drag effect (Gallery)

The drag effect is an indispensable function of smartphones. The drag effect is implemented on the Android platform, mainly using the gallery control. gallery indicates the gallery in English, as the name implies, the container stores the displayed images. The image storage resource container is a derived class inherited from the baseadapter class. Of course, you can also use setonitemclicklistener to listen to its events.

[Note] the gallery component is generally used to display the image list. Therefore, the difference between the gallery component and the gridview is that gallery can only display one row horizontally and supports horizontal sliding. That is to say, when you click, select, or drag an image in the gallery, the image list in the gallery moves left or right based on different situations until the last image is displayed.

 

Instance analysis: Based on the above introduction, you can store the image resource index to be displayed in an int array and specify the image to be displayed. You can use the setimageresource method to set the image to be displayed in the imageview.

Key source code:

Main. xml layout file:

<?xml version="1.0" encoding="utf-8"?><Gallery xmlns:android="http://schemas.android.com/apk/res/android"   android:id="@+id/Gallery01"  android:layout_width="fill_parent"  android:layout_height="wrap_content"/>

Note: The following imageadapter class inherits from baseadapter and overwrites its method.

Public class imageadapter extends baseadapter {private contextmcontext; // defines context // defines an integer array, that is, the image source private integer [] mimageids = {R. drawable. img1, R. drawable. img2, R. drawable. img3, R. drawable. img4, R. drawable. img5, R. drawable. img6, R. drawable. img7, R. drawable. img8,}; Public imageadapter (context c) {This. mcontext = C;} // obtain the number of images. Public int getcount () {return mimageids. length ;}// obtain the image's position in the Image Library public object getitem (INT position) {return position ;}// obtain the image idpublic long getitemid (INT position) {return position ;} public View getview (INT position, view convertview, viewgroup parent {imageview = new imageview (mcontext); // set the imageview resource for imageview. setimageresource (mimageids [position]); // sets the layout image to 120x120 display imageview. setlayoutparams (new gallery. layoutparams (120,120); // sets the display proportion type imageview. setscaletype (imageview. scaletype. fit_center); Return imageview ;}}

In activity01, you only need to pay the adapter to the gallery object and set the listening event."

// Obtain the gallery object gallery G = (Gallery) This. findviewbyid (R. id. gallery01); // Add the imageadapter to the gallery object G. setadapter (New imageadapter (this); // sets the listening event G of the gallery object. setonitemclicklistener (New onitemclicklistener () {public void onitemclick (adapterview <?> Arg0, view arg1, int arg2, long arg3) {toast. maketext (examples_04_17activity.this, "you have selected" + (arg2 + 1) + "image", toast. length_short ). show ();}});

Instance effect:

2. Change the image (imageswitcher)

This function is similar to that of many image browsers. You need to set a viewswitcher when you use imageswitcher to implement this function. viewfactory. Pay attention to viewswitcher. viewfactory is an interface designed to create a view in viewswitcher. The makeview () method is used to display images.

Public method: public abstract ViewMakeview()

Create a new view to be added to the viewswitcher.

 

Key source code:

Private imageswitcher m_switcher; // create the imageswitcher object Private Static int Index = 0; // index Private Static final int button_down_id = 0x123456; // "next page" button idprivate static final int button_up_id = 0x123457; // "Previous Page" button idprivate static final int switcher_id = 0x123458; // idpublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); linearlayout main_view = new linearlayout (this); m_switcher = new imageswitcher (this); // create the imageswitcher object main_view.addview (m_switcher ); // Add the imageswitcher view m_switcher.setid (switcher_id) to the linear layout; // set the ID m_switcher.setfactory (this) of the imageswitcher object ); // set the data source m_switcher.setimageresource (imagelist [Index]) of the imageswitcher object; setcontentview (main_view); // set to display the preceding linear layout button next = new button (this ); // create the next button. setid (button_down_id); next. settext ("Next"); next. setonclicklistener (this); linearlayout. layoutparams Param = new linearlayout. layoutparams (100,100); main_view.addview (next, Param); button pre = new button (this); // create the "previous" button pre. setid (button_up_id); pre. settext ("previous"); pre. setonclicklistener (this); main_view.addview (PRE, Param);} // event listening, processing public void onclick (view v) {Switch (v. GETID () {Case button_down_id: // next index ++; If (index> = imagelist. length) {Index = 0 ;}// imageswitcher object Resource Index m_switcher.setimageresource (imagelist [Index]); break; Case button_up_id: // previous index --; If (index <0) {Index = imagelist. length-1;} // imageswitcher object Resource Index m_switcher.setimageresource (imagelist [Index]); break; default: break; }}@ overridepublic view makeview () {return New imageview (this); // display all images through imageview}

[Extension point] viewswitcher

I. Structure

Public class viewswitcher extends viewanimator

Java. Lang. Object

Android. View. View

Android. View. viewgroup

Android. widget. framelayout

Android. widget. viewanimator

Android. widget. viewswitcher

Directly subclass: imageswitcher and textswitcher

Ii. Overview

An animation is displayed during conversion between two views. There is a factory class for creating these views. You can use a factory to create these views, or you can create them yourself. A viewswitcher can contain only two subviews and only one view can be displayed at a time.

(Note: similar to the viewflipper class, but this class is not commonly used. Generally, two sub-classes imageswitcher are used to add an animation effect when converting an image, and textswitcher is used to add an animation effect when converting text; for an example, see imageswitcher instance and textswitcher instance in apidemos)

Iii. Internal class

Interface viewswitcher. viewfactory

Create a view in a viewswitcher

................

V. Public Methods

Public voidSetfactory(Viewswitcher. viewfactory factory)

Set the factory for generating the two views that will be switched in the view converter. You can also call addview (Android. View. View, Int, Android. View. viewgroup. layoutparams) twice to replace the factory method.

Parameter: factory is used to generate the view factory of the converter content.

Reference from: http://dev.10086.cn/cmdn/wiki/index.php? Doc-view-4764.html

Instance effect:

3. Grid View)

The arrangement of the grid view is similar to that of the matrix. The elements to be displayed must also be implemented using baseadapter. The applicable method is similar to the gallery control.

Key code:

// Obtain the gridview object gridview = (gridview) This. findviewbyid (R. id. gridview); // Add an element to gridviewgridview. setadapter (New imageadapter (this); gridview. setbackgroundresource (R. drawable. bg0); // event listening gridview. setonitemclicklistener (New onitemclicklistener () {@ overridepublic void onitemclick (adapterview <?> Parent, view V, int position, long ID) {toast. maketext (activity01.this, "you have selected" + (Position + 1) + "image", toast. length_short ). show ();}});

4. Scroll View)

The scroll view is mainly used to display a view where a page of data cannot be displayed.

Example Analysis: scrollview defines a linear layout, A textview and a button in a linear layout. Each time you click a button, a linear layout is added.

Key source code:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/ScrollView01"    android:layout_width="fill_parent"    android:layout_height="wrap_content"android:scrollbars="none" >...... </ScrollView>

 

In the button clicking event:

...... // Change the default focus to buttonview. setonkeylistener (mnewbuttonkeylistener); // send a message to scroll through mhandler. post (mscrolltobottom); Private runnable mscrolltobottom = new runnable () {@ override public void run () {int off = mlayout. getmeasuredheight ()-mscrollview. getheight (); If (Off> 0) {mscrollview. scrollto (0, off); // set the position to which the current view is rolled }}};

Learn P101 today

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.