Android开发学习
图片左右滑动效果,第七种:加载网络图片的切换,使用控件ViewFlipper
新建xml布局文件:activity_two.xml,代码内容:
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
android:id="@+id/viewflipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
android:id="@+id/ll_dot"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:paddingBottom="30dp" >
android:id="@+id/iv_1"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginLeft="0dp"
android:clickable="true"
android:contentDescription="@string/dot"
android:src="@layout/dot" />
android:id="@+id/iv_2"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginLeft="5dp"
android:clickable="true"
android:contentDescription="@string/dot"
android:src="@layout/dot" />
android:id="@+id/iv_3"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginLeft="5dp"
android:contentDescription="@string/dot"
android:src="@layout/dot" />
android:id="@+id/iv_4"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginLeft="5dp"
android:contentDescription="@string/dot"
android:src="@layout/dot" />
使用线性布局LinearLayout,资源文件中的图片index1.jpg,index2.jpg,index3.jpg,index4.jpg
实现SevenActivity.java代码:
package com.zhengdecai.slidingeffect;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.ViewFlipper;
/**
* 图片左右滑动效果-加载网络图片的切换
*
* @author 郑德才
*
*/
public class SevenActivity extends Activity implements OnGestureListener {
private int[] imgID = { R.drawable.index1, R.drawable.index2,
R.drawable.index3, R.drawable.index4 };
private int[] ivID = { R.id.iv_1, R.id.iv_2, R.id.iv_3, R.id.iv_4 };
private List ivs = new ArrayList();
private ViewFlipper flipper;
private GestureDetector detector;
private Handler nextHandler;
private static Bitmap[] mImageIds = new Bitmap[4];
private String BitMapUrl;
private boolean BitmapOk;
private Bitmap mybitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
if (mImageIds[0] == null) {
mImageIds[0] = getHttpBitmap("http://pic21.nipic.com/20120519/2707401_113738673000_2.jpg");
}
if (mImageIds[1] == null) {
mImageIds[1] = getHttpBitmap("http://pic21.nipic.com/20120519/2707401_114241501000_2.jpg");
}
if (mImageIds[2] == null) {
mImageIds[2] = getHttpBitmap("http://pic13.nipic.com/20110329/2707401_181240282000_2.jpg");
}
if (mImageIds[3] == null) {
mImageIds[3] = getHttpBitmap("http://pic3.nipic.com/20090708/2966823_142000073_2.jpg");
}
for (int i = 0; i < ivID.length; i++) {
ImageView im_iv = (ImageView) findViewById(ivID[i]);
ivs.add(im_iv);
}
detector = new GestureDetector(this);
flipper = (ViewFlipper) findViewById(R.id.viewflipper);
for (int i = 0; i < mImageIds.length; i++) {
ImageView imageView = new ImageView(this);
imageView.setImageBitmap(mImageIds[i]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
flipper.addView(imageView, new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
}
ivs.get(0).setEnabled(false);
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
nextHandler.sendMessage(new Message());
}
}, 2000, 2000);
nextHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
flipper.showNext();
dotChange(flipper.getDisplayedChild());
}
};
}
public Bitmap getHttpBitmap(String url) {
BitMapUrl = url;
BitmapOk = false;
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
URL myFileURL;
Bitmap bitmap = null;
try {
myFileURL = new URL(BitMapUrl);
// 获得连接
HttpURLConnection conn = (HttpURLConnection) myFileURL
.openConnection();
// 设置超时时间为6000毫秒,conn.setConnectionTiem(0);表示没有时间限制
conn.setConnectTimeout(6000);
// 连接设置获得数据流
conn.setDoInput(true);
// 不使用缓存
conn.setUseCaches(false);
conn.connect();
// 得到数据流
InputStream is = conn.getInputStream();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
// 解析得到图片
bitmap = BitmapFactory.decodeStream(is, null, options);
// 关闭数据流
is.close();
} catch (Exception e) {
e.printStackTrace();
}
mybitmap = bitmap;
BitmapOk = true;
}
}).start();
while (true) {
if (BitmapOk) {
break;
}
}
return mybitmap;
}
private void dotChange(int index) {
for (int i = 0; i < ivs.size(); i++) {
if (i == index) {
ivs.get(i).setEnabled(false);
} else {
ivs.get(i).setEnabled(true);
}
}
}
@Override
public boolean onDown(MotionEvent e) {
return false;
}
@Override
public void onShowPress(MotionEvent e) {
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
return false;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
return false;
}
@Override
public void onLongPress(MotionEvent e) {
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
if (e1.getX() - e2.getX() > 120) {
// 添加动画
this.flipper.setInAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_left_in));
this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_left_out));
View view = flipper.getChildAt(mImageIds.length - 1);
View view1 = flipper.getCurrentView();
// flipper.getDisplayedChild();
this.flipper.showNext();
dotChange(flipper.getDisplayedChild());
return true;
}// 从右向左滑动
else if (e1.getX() - e2.getX() < -120) {
this.flipper.setInAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_right_in));
this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_right_out));
this.flipper.showPrevious();
dotChange(flipper.getDisplayedChild());
return true;
}
return true;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return this.detector.onTouchEvent(event);
}
}
模拟器运行效果: