標籤:
1、viewpager在廣告頭裡經常運用到,它是android3後出現的控制項,在因此在添加的時候android.support.v4.view.ViewPager.class裡,因此在xml定義的時候需要加全名稱,同時注意不要尾碼名class。下面從demo裡學習viewpager的運用。
<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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.viewdemo.MainActivity$PlaceholderFragment" android:background="@android:color/darker_gray" > <android.support.v4.view.ViewPager android:id="@+id/img_view" android:layout_width="fill_parent" android:layout_height="200dp" /> <!-- 這裡background設定後6位為rgb的值,前面是透明度參數,33為半透明 --> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:background="#33000000" android:layout_alignBottom="@+id/img_view" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:textColor="@android:color/white" android:paddingTop="5dp" android:id="@+id/title" /> <!-- 這裡定義一個空的線性布局,在代碼裡加入點的ui , 注意gravity的運用是在容器裡控制項的排列,用於ui是ui在它布局裡的排列--> <LinearLayout android:id="@+id/dians" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center_horizontal" android:paddingBottom="10dp" android:layout_marginTop="2dp" > </LinearLayout> </LinearLayout> </RelativeLayout>
2、java的activity代碼:
public class MainActivity extends Activity {private ViewPager viewpager;private ArrayList <ImageView> imageviews;private int number;private View[] points;private LinearLayout dians;private String[] titles;private TextView title;private boolean flag=true;//標識應用程式是否為可見public Handler handler=new Handler(){@Overridepublic void handleMessage(Message msg) {int a=msg.what;points[a].setEnabled(true);a++;int c=a%number;viewpager.setCurrentItem(c);title.setText(titles[c]);points[c].setEnabled(false);super.handleMessage(msg);}}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); dians=(LinearLayout) findViewById(R.id.dians); title=(TextView) findViewById(R.id.title); number=5; initview(); viewpager.setOnPageChangeListener(new PagerLisetner()); setpointsOnclick(); Thread pagermove=new Thread(new ThreadpageChange()); pagermove.start(); } class ThreadpageChange implements Runnable {@Overridepublic void run() {while(true){synchronized (MainActivity.class) {if(flag){try {Thread.sleep(2000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}int a=viewpager.getCurrentItem();Message msg=new Message();msg.what=a;handler.sendMessage(msg);} elsetry {MainActivity.class.wait();} catch (InterruptedException e) {e.printStackTrace();}}}} } //頁面改變監聽器 class PagerLisetner implements OnPageChangeListener{@Overridepublic void onPageScrollStateChanged(int arg0) {}@Overridepublic void onPageScrolled(int arg0, float arg1, int arg2) {}@Overridepublic void onPageSelected(int arg0) {title.setText(titles[arg0]);for(int x=0;x<number;x++ ){View v=dians.getChildAt(x);if(x==arg0)v.setEnabled(false);elsev.setEnabled(true);}}}//設定點被點擊的監聽器 public void setpointsOnclick(){for(int x=0;x<number;x++){View v=points[x];v.setTag(x);v.setOnClickListener(new PointOnclickLinstener());}}//點被點擊的監聽器 class PointOnclickLinstener implements OnClickListener{@Overridepublic void onClick(View v) {System.out.println("點擊了。。。"+v.getTag());int index=(Integer) v.getTag();title.setText(titles[index]);points[index].setEnabled(true);viewpager.setCurrentItem(index);}} //初始化介面 public void initview() {//將每張圖片建立一個Imgeview,並存起來 int[] images={ R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e }; imageviews=new ArrayList<ImageView>(); for(int i=0;i<number;i++) { ImageView im=new ImageView(this); im.setBackground(getResources().getDrawable(images[i])); imageviews.add(im); } viewpager=(ViewPager) findViewById(R.id.img_view); viewpager.setAdapter(new VpAdapter()); //添加點 addpoints(); //初始化標題 addTitle(); title.setText(titles[0]); points[0].setEnabled(false); }//添加點public void addpoints(){points=new View[number];for(int i=0;i<number;i++) { View v=new View(this); //建立子視圖 LayoutParams pa=new LayoutParams(8, 8); pa.leftMargin=10; v.setLayoutParams(pa);//使點按照子視圖格式形成view v.setBackgroundResource(R.drawable.bg);//設定點的背景,這裡的bg是選取器,下面介紹 v.setEnabled(true); //將建立的子視圖點添加到LinearLayout中 points[i]=v; dians.addView(v); }}//添加標題public void addTitle(){titles=new String[]{"鞏俐不低俗,我就不能低俗","樸樹郵回來啦!唱經典老歌引萬人大合唱","揭秘北京電影如何升級","樂視網TV版大派送","熱血屌絲的反殺"};}class VpAdapter extends PagerAdapter{@Overridepublic Object instantiateItem(ViewGroup container, int position) {viewpager.addView(imageviews.get(position));return imageviews.get(position);}@Overridepublic void destroyItem(ViewGroup container, int position, Object object) {//從右向左劃就刪除當前頁,從右往左就加,即執行instantiateItem,當1時候一下跳到5時,其實add了前面4個(僅我的看法)viewpager.removeView(imageviews.get(position));}@Overridepublic int getCount() {return imageviews.size();}@Overridepublic boolean isViewFromObject(View arg0, Object arg1) {return arg0==arg1;}}//重寫下面activity生命週期是當程式後台時為了將資源消耗,讓子線程等待@Overrideprotected void onResume() {if(!flag)MainActivity.class.notify();super.onResume();}@Overrideprotected void onPause() {flag=false;super.onPause();}}
3、下面介紹如何在xml檔案裡畫圖的,首先給出我的工程目錄:
可以看出,檔案裡定義了dian_normal,和dian_select,這是點在被選擇和沒有被選擇的時候的映像,bg其實就是這兩種映像的選取器,當可點擊時候選normal,不可點擊時候顯示select。畫圖是在drawable裡選擇shape,然後再裡面可以畫幾種簡單的形狀:
normal的xml檔案:android:shape=oval是指畫圓,下面的corners 裡radius是圓半徑、solid是填充顏色。
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <corners android:radius="0.5dp" /> <solid android:color="@android:color/background_dark" /> </shape>
select的xml檔案
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <corners android:radius="0.5dp" /> <solid android:color="@android:color/white" /></shape>
下面是選取器:db.xml,即當enable=true時候選擇一種drawable,false時候選擇另一個
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_enabled="false" android:drawable="@drawable/dian_select"></item><item android:state_enabled="true" android:drawable="@drawable/dian_normal"></item></selector>
自訂控制項實現廣告頭自動移動,viewpager的運用,和自己利用xml畫圖,自訂圓點,構造選取器的方法