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

当我们的自己的写的软件中,要进行一段耗时的工作时,或者进软件时需要从服务器上获取数据时,因为耗时,这时全屏加载转圈动画就能很好的给用户带来更好的体验性。有时候你设计的软件整体风格都是蓝色,这时我们使用系统那黑黑的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.javaR.style.model_dialog大家可以从下一篇博客《

Android之自定义对话框

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值