Realize the picture carousel, and with Dot, turn the dot change color when carousel;

Source: Internet
Author: User

Package com.example.day14;

Import java.util.ArrayList;
Import Java.util.Timer;
Import Java.util.TimerTask;

Import Com.lidroid.xutils.BitmapUtils;

Import Android.os.Bundle;
Import Android.os.Handler;
Import android.app.Activity;
Import Android.support.v4.view.PagerAdapter;
Import Android.support.v4.view.ViewPager;
Import Android.view.LayoutInflater;
Import Android.view.Menu;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.ImageView;
Import Android.widget.ImageView.ScaleType;
Import Android.widget.LinearLayout;

public class Mainactivity extends Activity {
int count = 0;
int olddotindex = 0;
Picture data
Private string[] Image_url = new string[]{
"Http://pic8.nipic.com/20100701/5290458_114840036316_2.jpg",
"Http://img3.3lian.com/2013/s1/20/d/57.jpg",
"Http://pic39.nipic.com/20140226/18071023_164300608000_2.jpg",
"Http://a0.att.hudong.com/15/08/300218769736132194086202411_950.jpg"};
Handler handler=new Handler () {
public void Handlemessage (Android.os.Message msg) {

Pager.setcurrentitem (count);
Dots.get (Olddotindex). Setbackgroundresource (R.drawable.dot_nomal);
Dots.get (count). Setbackgroundresource (R.drawable.dot_focus);
Olddotindex=count;


};
};
Private arraylist<view> dots;
Private LinearLayout LA;
private arraylist<imageview> images;
Private Viewpager Pager;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);

Init ();

}
private void init () {
TODO auto-generated Method Stub
Pager = (Viewpager) Findviewbyid (R.id.viewpager);
La = (linearlayout) Findviewbyid (R.id.yuandian);
Get network images, configure to Veiwpager
Getimageviewlist ();
Get Polka Dots
Getdotlist ();
Set the first dot as selected
Dots.get (0). Setbackgroundresource (R.drawable.dot_focus);
Pager.setadapter (New Myvpadapter ());
Auto-scrub effect via timer
Timer timer=new timer ();
Timer.schedule (New TimerTask () {

@Override
public void Run () {
TODO auto-generated Method Stub
count++;
if (count==4) {
count=0;
}

Handler.sendemptymessage (count);
}
}, 3000, 2000);
}
Get Polka Dots
private void Getdotlist () {
Dots = new arraylist<view> ();
loop a picture array and create a corresponding number of dot
for (int i = 0; i < image_url.length; i++) {
View View=layoutinflater.from (this). Inflate (R.layout.dot, NULL);//load layout;
Get the dot point component in the layout
View Dot=view.findviewbyid (R.id.dotview);
Collect dot;
Dots.add (dot);
Add layouts to linear layouts
La.addview (view);
}
}
Get network images, configure to Veiwpager
private void Getimageviewlist () {
Images = new arraylist<imageview> ();
Bitmaputils bitmaputils=new bitmaputils (this);
Loading pictures;
for (int i = 0; i < image_url.length; i++) {
ImageView imageview=new ImageView (this);
Imageview.setscaletype (SCALETYPE.FIT_XY);
Bitmaputils.display (ImageView, image_url[i]);
Images.add (ImageView);
}



}
Class Myvpadapter extends pageradapter{

@Override
public int GetCount () {
TODO auto-generated Method Stub
return Images.size ();
}

@Override
public boolean isviewfromobject (View arg0, Object arg1) {
TODO auto-generated Method Stub
return ARG0==ARG1;
}
@Override
public void Destroyitem (ViewGroup container, int position, object object) {
TODO auto-generated Method Stub

ImageView Im=images.get (position);
Container.removeview (IM);

}
@Override
Public Object Instantiateitem (viewgroup container, int position) {
TODO auto-generated Method Stub
Container.addview (Images.get (position));
return Images.get (position);


}
}
}

Create a drawable folder under Res;

Create 2 XML files inside the drawable;

One is dot_focus.xml;

<?xml version= "1.0" encoding= "Utf-8"?>
<shape xmlns:android= "Http://schemas.android.com/apk/res/android" >
<solid android:color= "#f00"/>
<corners android:radius= "8dip"/>

</shape>

One is dot_nomal.xml;

<?xml version= "1.0" encoding= "Utf-8"?>
<shape xmlns:android= "http://schemas.android.com/apk/res/android" android:shape= "Rectangle" >
<solid android:color= "#ffffff"/>
<corners android:radius= "8dip"/>
</shape>

The layout of the Acitivity_main.xml;

<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"
Tools:context= ". Mainactivity ">
<android.support.v4.view.viewpager
Android:id= "@+id/viewpager"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"

/>
<linearlayout
Android:id= "@+id/yuandian"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:gravity= "Center"
Android:layout_alignparentbottom= "true"
></LinearLayout>
</RelativeLayout>

Build a dot.xml in layout;

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:orientation= "Vertical" >
<view
Android:id= "@+id/dotview"
Android:layout_width= "8DP"
android:layout_height= "8DP"
Android:layout_margin= "10DP"
android:background= "@drawable/dot_nomal"
/>

</LinearLayout>

Realize the picture carousel, and with Dot, turn the dot change color when carousel;

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.