Android 欢迎界面设计之图片渐隐效果

设计一款Android手机应用软件,欢迎界面必不可少,今天我将设计一个欢迎界面图片渐隐效果

第一步:在主界面实现消息处理器

MainView.java

public class MainView extends Activity{

//屏幕宽度

public static int screenWidth;

//屏幕高度

public static int screenHeight;

//欢迎界面对象

Welcome_View welcome;

Handler hd = new Handler(){

//重写handleMessage方法

public void handleMessage(Message msg){

switch(msg.what){

case 0:

//跳转到welcome_view

goToWelcomeView();

break;

case 1:

//跳转到main_view

goToMainView();

break;

}

}

};

public void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);

//去除标题栏

requestWindowFeature(Window.FEATURE_NO_TITLE);

//去除状态栏

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,

WindowManager.LayoutParams.FLAG_FULLSCREEN);

//创建DisplayMetrics对象,用于获取屏幕宽度和高度

DisplayMetrics dm = new DisplayMetrics();

getWindowManager().getDefaultDisplay().getMetrics(dm);

//屏幕宽度

screenWidth = dm.widthPixels;

//屏幕高度

screenHeight = dm.heightPixels;

//发送消息,交由handler处理

this.hd.sendEmptyMessage(0);

}

//跳转到欢迎界面函数

private void goToWelcomeView(){

if(welcome == null){

//welcome为空,新建一个

welcome = new Welcome_View(this);

}

//加载welcome,其实欢迎界面并不需要配置文件,直接加载类

setContentView(welcome);

}

//跳转到MainView

private void goToMainView(){

//创建Intent对象

Intent intent = new Intent();

//Main_view自己创建,就是一个新的activity,此处不坠余

intent.setClass(InitActivity.this, Main_View.class);

//开始activity跳转

startActivity(intent);

//关闭当前Activity

this.finish();

}

}

第二步:实现Welcome_View.java

Welcome_View.java

public class Welcome_View extends SurfaceView

implements SurfaceHolder.Callback{

//activity引用

MainView activity;

//画笔

Paint paint;

//屏幕宽度

int screenWidth = MainView.screenWidth;

//屏幕高度

int screenHeight =MainView.screenHeight;

//当前不透明度

int currentAlpha = 0;

//休眠时间

int sleepTime = 50;

//图片数组

Bitmap[] logos = new Bitmap[2];

//当前logo

Bitmap currentLogo;

//X坐标

int currentX;

//Y坐标

int currentY;

public Welcome_View(MainView activity) {

super(activity);

this.activity = activity;

//设置生命周期回调接口实现者

this.getHolder().addCallback(this);

paint = new Paint();

//打开抗锯齿

paint.setAntiAlias(true);

//加载图片

logos[0] = BitmapFactory.decodeResource(activity.getResources(), R.drawable.surface1);

logos[1] = BitmapFactory.decodeResource(activity.getResources(), R.drawable.surface2);

}

public void onDraw(Canvas canvas){

paint.setColor(Color.BLACK);

paint.setAlpha(255);

//绘制填充矩形清晰界面

canvas.drawRect(0, 0,screenWidth,screenHeight,paint);

if(currentLogo == null) return;

paint.setAlpha(currentAlpha);

//平面贴图

canvas.drawBitmap(currentLogo, currentX,currentY, paint);

}

//界面创建时调用

@Override

public void surfaceCreated(SurfaceHolder arg0) {

new Thread(){

@SuppressLint("WrongCall")

public void run(){

for(Bitmap bm : logos){//对数组进行循环

//初始化当前图片

currentLogo = bm;

//图片位置

currentX = (screenWidth - bm.getWidth())/2;

currentY = (screenHeight - bm.getHeight())/2;

for(int i = 255;i > -10;i = i-10){

//设置当前不透明度

currentAlpha = i;

if(currentAlpha < 0){

currentAlpha = 0;

}

//获取回调接口

SurfaceHolder myholder = Welcome_View.this.getHolder();

//获取画布

Canvas canvas = myholder.lockCanvas();

try{

synchronized(myholder){

onDraw(canvas);

}

}catch(Exception e){

e.printStackTrace();

}finally{

if(canvas != null){

myholder.unlockCanvasAndPost(canvas);

}

}

try{

if(i == 255){

Thread.sleep(1000);

}

Thread.sleep(sleepTime);

}catch(Exception e){

e.printStackTrace();

}

}

}

//发送消息,交由Handler处理,跳转到主界面

activity.hd.sendEmptyMessage(1);

}

}.start();

}

@Override

public void surfaceChanged(SurfaceHolder holder, int format, int width,

int height) {

// TODO Auto-generated method stub

}

@Override

public void surfaceDestroyed(SurfaceHolder holder) {

// TODO Auto-generated method stub

}
}

你可能感兴趣的:(Android)