Android仿硬幣轉(zhuǎn)動(dòng)微信紅包動(dòng)畫(huà)效果
項(xiàng)目需要研究了一下微信紅包動(dòng)畫(huà),即硬幣轉(zhuǎn)動(dòng)的效果,原理其實(shí)就是三張不同角度的圖片利用AnimationDrawable幀動(dòng)畫(huà)進(jìn)行播放,在參考了案例之后,給自己記錄一下完成的過(guò)程。
1,在XML文件中定義動(dòng)畫(huà):
步驟如下:
①新建 Android 項(xiàng)目
②在drawable目錄中新建一個(gè)anim.xml(注意文件名小寫(xiě))
<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> <item android:drawable="@drawable/bag" android:duration="400"></item> <item android:drawable="@drawable/bag1" android:duration="400"></item> <item android:drawable="@drawable/bag2" android:duration="400"></item> </animation-list>
根標(biāo)簽為animation-list,其中oneshot代表著是否只展示一遍,設(shè)置為false會(huì)不停的循環(huán)播放動(dòng)畫(huà)根標(biāo)簽下,通過(guò)item標(biāo)簽對(duì)動(dòng)畫(huà)中的每一個(gè)圖片進(jìn)行聲明 ,android:duration 表示展示所用的該圖片的時(shí)間長(zhǎng)度 ,可通過(guò)該參數(shù)來(lái)設(shè)置圖片旋轉(zhuǎn)的速度,其他屬性可以自行查找資料~
2,設(shè)置布局文件,效果以及代碼如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical|center_horizontal"
android:background="@drawable/background">
<!-- 關(guān)閉按鈕框 -->
<LinearLayout
android:id="@+id/top"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:id="@+id/close"
android:layout_width="32dp"
android:layout_height="32dp"
android:background="@drawable/close"
android:layout_margin="10dp"/>
</LinearLayout>
<!-- 頭像以及相關(guān)文字 -->
<LinearLayout
android:layout_below="@+id/top"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="10"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3">
<ImageButton
android:id="@+id/head_img"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="@drawable/ic_launcher"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="系統(tǒng)用戶(hù)"
android:layout_marginTop="10dp"
android:layout_below="@+id/head_img"
android:layout_centerHorizontal="true"
android:textColor="@color/yellow"
android:textSize="18sp"/>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/name"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:textSize="15sp"
android:textColor="@color/yellow2"
android:text="給你發(fā)了一個(gè)紅包"/>
<TextView
android:id="@+id/textView2"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:textColor="@color/yellow"
android:textSize="23sp"
android:text="恭喜發(fā)財(cái),大吉大利"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3">
<Button
android:id="@+id/open_btn"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@drawable/anim"
android:layout_marginTop="50dp"
android:layout_centerHorizontal="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/blow"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="14dp"
android:id="@+id/imageView" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
3,實(shí)現(xiàn)紅包彈窗的效果,效果及代碼如下:
步驟如下:
①自定義紅包彈窗Diaog類(lèi):紅色代碼部分為啟動(dòng)動(dòng)畫(huà)部分

package com.example.xuboyu.luckeymoney;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.AnimationDrawable;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
/**
* 自定義紅包彈窗
* Created by xuboyu on 2017/2/20.
*/
public class LuckeyDialog extends Dialog {
public LuckeyDialog(Context context) {
super(context);
}
public LuckeyDialog(Context context, int theme) {
super(context, theme);
}
public static class Builder {
private Context context;
private String name;//發(fā)紅包者的名稱(chēng)
private Button red_page;
//拆紅包按鈕
private String openButtonText;
private OnClickListener openButtonClickListener;
//關(guān)閉按鈕
private String closeButtonText;
private OnClickListener closeButtonClickListener;
public Builder(Context context, int dialog) {
this.context = context;
}
/**
* Set the Dialog title from resource
*
* @param name
* @return
*/
public Builder setName(int name) {
this.name = (String) context.getText(name);
return this;
}
/**
* Set the Dialog title from String
*
* @param name
* @return
*/
public Builder setName(String name) {
this.name = name;
return this;
}
/**
* Set the positive button resource and it's listener
*
* @param closeButtonText
* @return
*/
public Builder setCloseButton(int closeButtonText,
OnClickListener listener) {
this.closeButtonText = (String) context
.getText(closeButtonText);
this.closeButtonClickListener = listener;
return this;
}
public Builder setCloseButton(String closeButtonText,
OnClickListener listener) {
this.closeButtonText = closeButtonText;
this.closeButtonClickListener = listener;
return this;
}
/**
* Set the positive button resource and it's listener
*
* @param openButtonText
* @return
*/
public Builder setOpenButton(int openButtonText,
OnClickListener listener) {
this.openButtonText = (String) context
.getText(openButtonText);
this.openButtonClickListener = listener;
return this;
}
public Builder setOpenButton(String openButtonText,
OnClickListener listener) {
this.openButtonText = openButtonText;
this.openButtonClickListener = listener;
return this;
}
public LuckeyDialog create() {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//加載布局
final LuckeyDialog dialog = new LuckeyDialog(context,R.style.Dialog);
View layout = inflater.inflate(R.layout.open, null);
red_page = (Button) layout.findViewById(R.id.open_btn);
<span style="color:#ff0000;">//red指的是需要播放動(dòng)畫(huà)的ImageView控件
AnimationDrawable animationDrawable = (AnimationDrawable)red_page.getBackground();
animationDrawable.start();//啟動(dòng)動(dòng)畫(huà)</span>
dialog.addContentView(layout, new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
//設(shè)置發(fā)紅包者姓名
((TextView) layout.findViewById(R.id.name)).setText(name);
//設(shè)置拆紅包的按鈕
if (openButtonText != null) {
((Button) layout.findViewById(R.id.open_btn))
.setText(openButtonText);
if (openButtonClickListener != null) {
((Button) layout.findViewById(R.id.open_btn))
.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
openButtonClickListener.onClick(dialog,
DialogInterface.BUTTON_POSITIVE);
}
});
}
} else {
// if no confirm button just set the visibility to GONE
layout.findViewById(R.id.open_btn).setVisibility(
View.GONE);
}
//設(shè)置關(guān)閉按鈕
if (closeButtonText != null) {
((Button) layout.findViewById(R.id.close))
.setText(closeButtonText);
if (closeButtonClickListener != null) {
((Button) layout.findViewById(R.id.close))
.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
closeButtonClickListener.onClick(dialog,
DialogInterface.BUTTON_POSITIVE);
}
});
}
} else {
// if no confirm button just set the visibility to GONE
layout.findViewById(R.id.close).setVisibility(
View.GONE);
}
dialog.setContentView(layout);
return dialog;
}
}
}
②在系統(tǒng)style文件中新增一個(gè)Diaog
<style name="Dialog" parent="android:style/Theme.Dialog"> <!-- <item name="android:background">#00000000</item> --> <item name="android:windowBackground">@drawable/red_bg</item> <item name="android:windowFrame">@null</item> <item name="android:windowNoTitle">true</item> <item name="android:windowIsFloating">true</item><!-- 是否漂現(xiàn)在activity上 --> <item name="android:windowCloseOnTouchOutside">false</item </style>
③在MainActivity中調(diào)用自定義的Diaog類(lèi)并實(shí)例化,并且設(shè)置彈出的紅包占屏幕的比例,不然彈出的紅包會(huì)占滿(mǎn)整個(gè)屏幕,紅色代碼為設(shè)置大小代碼。
red1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LuckeyDialog.Builder builder = new LuckeyDialog.Builder(mContext,R.style.Dialog);//調(diào)用style中的Diaog
builder.setName("系統(tǒng)");
builder.setOpenButton("", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(mContext,Open.class);
startActivity(intent);
dialog.dismiss();
}
});
builder.setCloseButton("", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int i) {
dialog.dismiss();
}
});
<span style="color:#ff0000;">Dialog dialog = builder.create();
Window dialogWindow = dialog.getWindow();
WindowManager m = getWindowManager();
Display d = m.getDefaultDisplay(); // 獲取屏幕寬、高用
WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 獲取對(duì)話(huà)框當(dāng)前的參數(shù)值
p.height = (int) (d.getHeight() * 0.7); // 高度設(shè)置為屏幕的0.6
p.width = (int) (d.getWidth() * 0.75); // 寬度設(shè)置為屏幕的0.65
dialogWindow.setAttributes(p);
</span>
dialog.show();
}
});
4,完成點(diǎn)擊后的兩種結(jié)果,即搶到和未搶到的兩種結(jié)果,通過(guò)Intent跳轉(zhuǎn)領(lǐng)取成功類(lèi)或者跳出失敗彈窗的簡(jiǎn)單邏輯即可。
①搶到的效果圖,這里界面比較簡(jiǎn)單就不貼代碼了。

②失敗彈窗的效果圖,這里的自定義彈窗代碼與紅包彈窗的代碼基本相似,區(qū)別就在于少了個(gè)拆紅包按鈕而已,布局也相對(duì)簡(jiǎn)單,就不貼出來(lái)了,主要在這里面需要使用比例來(lái)規(guī)劃幾個(gè)部件的位置(參考上面的紅包代碼),否則無(wú)法適配多種屏幕,會(huì)出現(xiàn)壓縮拉伸變形的情況。

到這里粗略的紅包動(dòng)畫(huà)效果就基本完成了!當(dāng)然實(shí)際應(yīng)用中需要用到網(wǎng)絡(luò)請(qǐng)求之類(lèi)的,就再按照業(yè)務(wù)要求加入。
以上所述是小編給大家介紹的Android仿硬幣轉(zhuǎn)動(dòng)微信紅包動(dòng)畫(huà)效果,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- 通過(guò)FancyView提供 Android 酷炫的開(kāi)屏動(dòng)畫(huà)實(shí)例代碼
- Android自定義帶加載動(dòng)畫(huà)效果的環(huán)狀進(jìn)度條
- Android自定義viewGroup實(shí)現(xiàn)點(diǎn)擊動(dòng)畫(huà)效果
- Android仿打開(kāi)微信紅包動(dòng)畫(huà)效果實(shí)現(xiàn)代碼
- Android動(dòng)畫(huà)入門(mén)教程之kotlin
- Android實(shí)現(xiàn)過(guò)渡動(dòng)畫(huà)、引導(dǎo)頁(yè) Android判斷是否第一次啟動(dòng)App
- Android 仿余額寶數(shù)字跳動(dòng)動(dòng)畫(huà)效果完整代碼
- Android開(kāi)發(fā)簡(jiǎn)單實(shí)現(xiàn)搖動(dòng)動(dòng)畫(huà)的方法
- Android開(kāi)發(fā)之背景動(dòng)畫(huà)簡(jiǎn)單實(shí)現(xiàn)方法
- Android動(dòng)畫(huà)實(shí)現(xiàn)原理和代碼
相關(guān)文章
Android DrawerLayout布局與NavigationView導(dǎo)航菜單應(yīng)用
這篇文章主要介紹了Android DrawerLayout抽屜布局與NavigationView導(dǎo)航菜單應(yīng)用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-01-01
android動(dòng)態(tài)壁紙調(diào)用的簡(jiǎn)單實(shí)例
動(dòng)態(tài)壁紙的實(shí)現(xiàn)其實(shí)就是在Activity中調(diào)用動(dòng)態(tài)壁紙服務(wù),通過(guò)綁定服務(wù)得到IWallpaperService,調(diào)用該接口中的attach函數(shù)實(shí)現(xiàn)壁紙的調(diào)用。2013-06-06
Android Studio使用Kotlin時(shí),修改代碼后運(yùn)行不生效的解決方法
這篇文章主要介紹了Android Studio使用Kotlin時(shí),修改代碼后運(yùn)行不生效的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
android RecycleView實(shí)現(xiàn)下拉刷新和上拉加載
這篇文章主要為大家詳細(xì)介紹了android RecycleView實(shí)現(xiàn)下拉刷新和上拉加載,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06
深入探究Android動(dòng)態(tài)代理的原理及用途
動(dòng)態(tài)代理是一種在編程中非常有用的設(shè)計(jì)模式,它允許你在運(yùn)行時(shí)創(chuàng)建一個(gè)代理對(duì)象來(lái)代替原始對(duì)象,以便在方法調(diào)用前后執(zhí)行額外的邏輯,本文將深入探討Android動(dòng)態(tài)代理的原理、用途和實(shí)際示例2023-09-09
Android之側(cè)滑菜單DrawerLayout的使用介紹
本篇文章主要介紹了Android之側(cè)滑菜單DrawerLayout的使用介紹,可以生成通過(guò)在屏幕上水平滑動(dòng)打開(kāi)或者關(guān)閉菜單,能給用戶(hù)一個(gè)不錯(cuò)的體驗(yàn)效果2017-02-02
Android scrollview實(shí)現(xiàn)底部繼續(xù)拖動(dòng)查看圖文詳情
這篇文章主要為大家詳細(xì)介紹了Android scrollview實(shí)現(xiàn)底部繼續(xù)拖動(dòng)查看圖文詳情,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02
windows10安裝adb/fastboot驅(qū)動(dòng)超詳細(xì)圖文教程
這篇文章主要介紹了windows10安裝adb/fastboot超詳細(xì)圖文教程,安裝方法也很簡(jiǎn)單,只要adb安裝成功,fastboot就安裝好了,文中給大家介紹了問(wèn)題分析及解決方法,需要的朋友可以參考下2023-01-01

