編輯:關於Android編程
今天用到viewpager,要實現多view動畫切換。自己動手做了一個。
先上效果圖,只是很簡單的例子。
步驟:1、在main布局文件裡添加viewPager布局<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PHByZSBjbGFzcz0="brush:java;">
再創建3個layout用於填充在ViewPager。我這裡就是一個textview而已。
2、viewPager需要一個pagerAdapter的子類。
package com.away.viewpager; import java.util.List; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.view.View; public class ViewPagerAdapter extends PagerAdapter { List3、最後mainActivity,主要寫了左右滑動切換頁面,還有一個小圖片隨頁面切換位移的動畫效果。viewLists; public ViewPagerAdapter(List lists) { viewLists = lists; } //獲得size @Override public int getCount() { return viewLists.size(); } @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == arg1; } //銷毀Item @Override public void destroyItem(View view, int position, Object object) { ((ViewPager) view).removeView(viewLists.get(position)); } //實例化Item @Override public Object instantiateItem(View view, int position) { ((ViewPager) view).addView(viewLists.get(position), 0); return viewLists.get(position); } }
package com.away.viewpager; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.os.Bundle; import android.support.v4.view.ViewPager; import android.util.DisplayMetrics; import android.view.Menu; import android.view.View; import android.view.animation.Animation; import android.view.animation.TranslateAnimation; import android.widget.ImageView; import android.widget.TextView; public class MainActivity extends Activity { private ViewPager viewPager; private ImageView imageView; private List最後附上光標圖。lists = new ArrayList (); private ViewPagerAdapter adapter; private Bitmap cursor; private int offSet; private int currentItem; private Matrix matrix = new Matrix(); private int bmWidth; private Animation animation; private TextView textView1; private TextView textView2; private TextView textView3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView = (ImageView) findViewById(R.id.cursor); textView1 = (TextView) findViewById(R.id.textView1); textView2 = (TextView) findViewById(R.id.textView2); textView3 = (TextView) findViewById(R.id.textView3); lists.add(getLayoutInflater().inflate(R.layout.layout1, null)); lists.add(getLayoutInflater().inflate(R.layout.layout2, null)); lists.add(getLayoutInflater().inflate(R.layout.layout3, null)); initeCursor(); adapter = new ViewPagerAdapter(lists); viewPager = (ViewPager) findViewById(R.id.viewPager); viewPager.setAdapter(adapter); viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { // 當滑動式,頂部的imageView是通過animation緩慢的滑動 @Override public void onPageSelected(int arg0) { switch (arg0) { case 0: if (currentItem == 1) { animation = new TranslateAnimation( offSet * 2 + bmWidth, 0, 0, 0); } else if (currentItem == 2) { animation = new TranslateAnimation(offSet * 4 + 2 * bmWidth, 0, 0, 0); } break; case 1: if (currentItem == 0) { animation = new TranslateAnimation(0, offSet * 2 + bmWidth, 0, 0); } else if (currentItem == 2) { animation = new TranslateAnimation(4 * offSet + 2 * bmWidth, offSet * 2 + bmWidth, 0, 0); } break; case 2: if (currentItem == 0) { animation = new TranslateAnimation(0, 4 * offSet + 2 * bmWidth, 0, 0); } else if (currentItem == 1) { animation = new TranslateAnimation( offSet * 2 + bmWidth, 4 * offSet + 2 * bmWidth, 0, 0); } } currentItem = arg0; animation.setDuration(150); // 光標滑動速度 animation.setFillAfter(true); imageView.startAnimation(animation); } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { } @Override public void onPageScrollStateChanged(int arg0) { } }); textView1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { viewPager.setCurrentItem(0); } }); textView2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { viewPager.setCurrentItem(1); } }); textView3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { viewPager.setCurrentItem(2); } }); } private void initeCursor() { cursor = BitmapFactory.decodeResource(getResources(), R.drawable.cursor); bmWidth = cursor.getWidth(); DisplayMetrics dm; dm = getResources().getDisplayMetrics(); offSet = (dm.widthPixels - 3 * bmWidth) / 6; matrix.setTranslate(offSet, 0); imageView.setImageMatrix(matrix); // 需要iamgeView的scaleType為matrix currentItem = 0; } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
上篇文章介紹了Android開發的設計理念的一部分,並沒有得到博友們的多大認可,只看到了一位博友在下面留言期待下一篇文章的發表,為了這小小的唯一支持,我決定繼續把後面的8
(一)概述本節開始講解Android的四大組件之一的Activity(活動);官方文檔:Activity是一個應用程序的組件,他在屏幕上提供了一個區域,允許用戶在上面做一
著手開發一款應用的時候,設置或者菜單頁面是可能需要的,但是,那重復的布局會很令人苦惱。新手可能會一項項的重復繪制,有經驗的你或許會用到include,或者用到組合控件。除
在移動互聯網,鏈接是比較重要的傳播媒質,但很多時候我們又希望用戶能夠回到APP中,這就要求APP可以通過浏覽器或在微信中被方便地喚起。這是一個既直觀又很好的用戶體驗,但在