您的位置:首页 > 移动开发 > Android开发

Android之全屏转圈加载动画和自定义Toast

2014-10-18 14:56 330 查看
当我们的自己的写的软件中,要进行一段耗时的工作时,或者进软件时需要从服务器上获取数据时,因为耗时,这时全屏加载转圈动画就能很好的给用户带来更好的体验性。有时候你设计的软件整体风格都是蓝色,这时我们使用系统那黑黑的Toast给用户提示时就显得有点不搭这个软件的风格,这时自定义Toast就派上用场了。这次我将在上一次的基础上,带领大家来完成全屏加载转圈动画和自定义Toast。下面给出今天的两张完成后的效果图:

全屏加载转圈动画



自定义Toast



这两个都效果做起来都不是很难,转圈动画我的做法是使用一种圆圈的图片作为ImageView的背景图,给它转圈动画,并将次ImageView加载到自定义对话框中就可以了。自定义Toast则跟自定义对话框有点类似,加载自定义view,修改其属性就ok了。下面给出代码:

布局文件:main.xml

<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=".MainActivity" >

<Button

android:id="@+id/button"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:onClick="Click"

android:text="弹出自定义对话框"

/>

<Button

android:id="@+id/button1"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_below="@id/button"

android:onClick="Click"

android:text="弹出加载对话框"

/>

<Button

android:id="@+id/button2"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:onClick="Click"

android:layout_below="@id/button1"

android:text="弹出自定义Toast"

/>

</RelativeLayout>

自己写的封装的Tools,也是最重要的,这个大家直接复制就可以使用,只需把图片替换成你们的图片:

package com.example.customdialog;

import android.app.Dialog;

import android.content.Context;

import android.graphics.Color;

import android.view.Gravity;

import android.view.View;

import android.view.ViewGroup.LayoutParams;

import android.view.WindowManager;

import android.view.animation.Animation;

import android.view.animation.LinearInterpolator;

import android.view.animation.RotateAnimation;

import android.widget.ImageView;

import android.widget.TextView;

import android.widget.Toast;

public class Tools {

public static Dialog createLoadingDialog(Context context) {

// View v = LayoutInflater.from(context).inflate(R.layout.prograss,

// null);

// Animation hyRotateAnimation = AnimationUtils.loadAnimation(context,

// R.anim.rotate);

// v.startAnimation(hyRotateAnimation);

ImageView mImageView = new ImageView(context);

mImageView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,

LayoutParams.WRAP_CONTENT));

mImageView.setBackgroundResource(R.drawable.dialog_program_icon);

startRotateAnimation(mImageView);

Dialog dialog = new Dialog(context, R.style.model_dialog);

dialog.setContentView(mImageView);

// dialog.setCancelable(false);//取消不可用

// dialog.setCanceledOnTouchOutside(false);//点击对话框外的事件无效

WindowManager.LayoutParams lp = new WindowManager.LayoutParams(

WindowManager.LayoutParams.MATCH_PARENT,

WindowManager.LayoutParams.MATCH_PARENT);// 全屏

lp.alpha = 0.0f;// 透明度 0.0-1.0,0.0完全透明,1.0完全不透明

lp.dimAmount = 0.0f;// 背景层 0.0-1.0,0.0背景完全可见,1.0背景全黑

dialog.setContentView(mImageView, lp);

return dialog;

}

/**

* 给视图View创建旋转动画

*

* @param v

*/

public static void startRotateAnimation(View v) {

RotateAnimation mRotateAnimation = new RotateAnimation(0, 360,

Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,

0.5f);

// 创建旋转动画对象,从0°旋转到360°,以自身中心点为旋转轴

mRotateAnimation.setDuration(1000);

// 从0°旋转到360°所需要的时间

mRotateAnimation.setInterpolator(new LinearInterpolator());

// 线性插入器(匀速旋转)

mRotateAnimation.setRepeatCount(-1);

// 重复次数,-1无限循环

mRotateAnimation.setRepeatMode(Animation.RESTART);

// 重复模式:restart重新开始

v.startAnimation(mRotateAnimation);

// 开始动画

}

/**

* 创建自定义Toast

*

* @param context

* 上下文

* @param str

* 要显示的内容

* @param xOffset

* x方向的偏移量

* @param yOffset

* y方向的偏移量

*/

public static void showToastInfo(Context context, String str, int xOffset,

int yOffset) {

TextView toastView = new TextView(context);

toastView.setTextSize(24);

toastView.setGravity(Gravity.CENTER);

toastView.setTextColor(Color.WHITE);

toastView.setText(str);

toastView.setBackgroundResource(R.drawable.toast_info_bg);

Toast toast = new Toast(context);

toast.setGravity(Gravity.CENTER, xOffset, yOffset);

toast.setDuration(1000);

toast.setView(toastView);

toast.show();

}

}

主Activity:MainActivity.java

package com.example.customdialog;

import android.os.Bundle;

import android.app.Activity;

import android.app.Dialog;

import android.view.Menu;

import android.view.View;

import android.widget.Button;

import android.widget.Toast;

public class MainActivity extends Activity {

private Button button;

private Button button1;

private Button button2;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

init();

}

private void init() {

button = (Button)findViewById(R.id.button);

button1 = (Button)findViewById(R.id.button1);

button2 = (Button)findViewById(R.id.button2);

}

public void Click(View view){

int id = view.getId();

switch (id) {

case R.id.button:

showDialog();

break;

case R.id.button1:

showLoadingDialog();

break;

case R.id.button2:

showToastInfo("开始运行");

break;

default:

break;

}

}

public void showDialog(){

final MyDialog dialog = new MyDialog(this,R.string.dialog_content);

dialog.setClickListener(new MyDialog.ClickListener() {

@Override

public void onConfirmClick(MyDialog myDialog) {

Toast.makeText(getApplicationContext(), "点击了确定按钮", Toast.LENGTH_SHORT).show();

dialog.cancel();

}

@Override

public void onCancleClick(MyDialog myDialog) {

Toast.makeText(getApplicationContext(), "点击了取消按钮", Toast.LENGTH_SHORT).show();

dialog.cancel();

}

});

dialog.show();

}

public void showLoadingDialog(){

Dialog dialogLoading = Tools.createLoadingDialog(this);

dialogLoading.show();

}

public void showToastInfo(String str){

Tools.showToastInfo(this, str, 0, 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;

}

}

Ps:上面的代码不是完整的项目代码,这个工程是在上一篇博客的基础上写的,Tools.java的R.style.model_dialog大家可以从上一篇博客中拷贝
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: