安卓图片切换
采用ImageSwitcher实现
主Activity如下:
package com.dsl.ui_application_01;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher;
public class MainActivity extends AppCompatActivity {
private int[] image={R.mipmap.anni,R.mipmap.aolafu,R.mipmap.baoshi,R.mipmap.jie,R.mipmap.ic_launcher};
int index=0;
double startX=0.0;
double endX=0.0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageSwitcher is=(ImageSwitcher)findViewById(R.id.imageSwitcher1);
is.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
ImageView iv = new ImageView(MainActivity.this);
iv.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
iv.setImageResource(image[index]);
return iv;
}
});
is.setOnTouchListener(new View.OnTouchListener(){
//触摸事件
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN)
{
startX=event.getX();
return true;//按下去
}
if(event.getAction()==MotionEvent.ACTION_UP)
{
endX=event.getX();
System.out.println("start="+startX);
System.out.println("endX="+endX);
if(endX-startX>20)//向右滑
{
index=index-1>0?--index:image.length-1;
System.out.println("向右滑");
is.setInAnimation(MainActivity.this,android.R.anim.fade_in);
is.setOutAnimation(MainActivity.this,android.R.anim.fade_out);
is.setImageResource(image[index]);
}
else if(startX-endX>20)//向左滑
{
index=index+1<image.length?++index:0;
System.out.println("向左滑");
is.setInAnimation(MainActivity.this,android.R.anim.fade_in);
is.setOutAnimation(MainActivity.this,android.R.anim.fade_out);
is.setImageResource(image[index]);
}
}
return false;
}
});
}
}
引用的主xml
<?xml version="1.0" encoding="utf-8"?>
<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.dsl.ui_application_01.MainActivity">
<ImageSwitcher
android:id="@+id/imageSwitcher1"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ImageSwitcher>
</RelativeLayout>
上面这里就是图片切换所有代码了,还有个是字体切换TextSwitcher和图片的用法是一样的,这里就不再做介绍了
下面这个是屏幕切换
主Activity如下:
package com.dsl.ui_application_01;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ViewFlipper;
public class MainActivity2 extends AppCompatActivity {
double startX=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewfilper);
final ViewFlipper vf=(ViewFlipper)findViewById(R.id.viewFlipper1);
vf.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN)
{
startX=event.getX();
return true;
}
else if(event.getAction()==MotionEvent.ACTION_UP)
{
if(event.getX()>startX)//右滑
{
System.out.println("右滑");
vf.showPrevious();
}else if(event.getX()<startX)//左滑
{
vf.showNext();
}
}
return false;
}
});
}
}
引用的.xml文件如下:
<?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"
>
<ViewFlipper
android:id="@+id/viewFlipper1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:id="@+id/linear1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@mipmap/anni"
android:maxWidth="100dp"
android:maxHeight="500dp"
android:adjustViewBounds="true"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/linear2"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@mipmap/aolafu"
android:scaleType="centerCrop"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/linear3"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@mipmap/baoshi"
android:scaleType="centerCrop"
/>
</LinearLayout>
</ViewFlipper>
</LinearLayout>
在这个工程中、第二个屏幕切换的Activity我没有在Manifest中注册,大家下载工程后,只需自行在Manifest中加入即可。
工程如下:
https://github.com/DSLAndroid/UI_Application_01
本资源来自单胜凌!!!
Android靠自学!!!
祝各位IT人士早日成功实现事业目标!!!