标签:
由于本人还是个菜鸡……代码质量难以保证,so仅供个人学习记录目的和仅供参考……要是发现什么bug会编辑博客……有意见建议欢迎提出!
最近在Android学习的过程中接触到了简单的动画效果,就突然想做个电子宠物应用一下(~ ̄▽ ̄)~*。
素材来源是游戏逆转裁判的素材……也算表达一下个人对这一系列的爱……(~ ̄▽ ̄)~*。
gif的显示用的是GitHub的开源库:android-gif-drawable,https://github.com/koral--/android-gif-drawable
实现之后的界面如下,左上角有喂食,清洁和玩耍三个选项,屏幕下方是角色活动区域,由于个人水平不够,目前只能按照规定的路线移动。然后在按钮右侧,采用了ProgressBar来显示角色当前的状态,状态由service后台更新。
首先,定义了一个存储状态,资源文件id,对话等的类,如下
package com.ldgforever.NZpetty; public class petState{ private static int mHunger = 100; private static int mCleaning = 100; private static int mHappiness = 100; public petState(){ mHunger = 100; mCleaning = 100; mHappiness = 100; } private static String[] mHungerDialog = { "你给我吃的是什么垃圾食品", "还不错的样子", "有点好吃", "吃什么好呢", "要不要一起吃??", "……哼", "……(饿了)" , "这……这是什么啊!" }; private static int[] hungerHeadImage = { R.drawable.mingcowhide, R.drawable.mingcross, R.drawable.mingonefinger, R.drawable.mingpointat, R.drawable.mingwave, R.drawable.mingcrossed, R.drawable.mingthreat, R.drawable.mingwtf }; private static String[] mCleanDialog = { "舒服多了", "……(愉悦)", "你看见我的小黄鸭了么", "……(心情不错)", "肥皂找不到了", "快去给本小姐烧水!" }; private static int[] cleanHeadImage = { R.drawable.mingpointat, R.drawable.mingcross, R.drawable.mingwave, R.drawable.mingonefinger, R.drawable.mingcrossed, R.drawable.mingcowhide }; private static String[] mPlayDialog = { "只有白痴的律师才会用白痴的思维询问白痴的证人从而得到白痴的答案……就让本人的皮鞭告诉你真相吧!", "……哼", "白痴看到白痴在白日梦里看到了另一个白痴的白痴……", "……(暴躁)", "这个月的工资问题可要仔细研究一下了……", "导弹?……还挺可爱的", "干的好,这一鞭是赏你的!!", "如果把我惹火了,我可以把一两个男的弄哭给你看!", "卑鄙、下流、肮脏、无耻、败类加人渣的臭男人", "就算你有自信,错误也是不会改变的!" }; private static int[] happinessHeadImage = { R.drawable.mingthreat, R.drawable.mingcrossed, R.drawable.mingwave, R.drawable.mingcowhide, R.drawable.mingpointat, R.drawable.mingonefinger, R.drawable.mingwave, R.drawable.mingcross, R.drawable.mingcowhide, R.drawable.mingpointat }; private static int[] onTouchImage = { R.drawable.minganger, R.drawable.mingmanner, R.drawable.mingfall, R.drawable.mingtada, R.drawable.mingandlang, R.drawable.minghuggy }; public static int getHunger() { return mHunger; } public static void setHunger(int hunger) { mHunger = hunger; } public static int getCleaning() { return mCleaning; } public static void setCleaning(int cleaning) { mCleaning = cleaning; } public static String getHungerDialog(int index) { return mHungerDialog[index]; } public void setHungerDialog(String[] hungerDialog) { mHungerDialog = hungerDialog; } public static String getCleanDialog(int index) { return mCleanDialog[index]; } public void setCleanDialog(String[] cleanDialog) { mCleanDialog = cleanDialog; } public static String getPlayDialog(int index) { return mPlayDialog[index]; } public void setPlayDialog(String[] playDialog) { mPlayDialog = playDialog; } public static int getHappiness() { return mHappiness; } public static void setHappiness(int happiness) { mHappiness = happiness; } public static int getHungerHeadImage(int index) { return hungerHeadImage[index]; } public void setHungerHeadImage(int[] hungerHeadImage) { this.hungerHeadImage = hungerHeadImage; } public static int getCleaningHeadImage(int index) { return cleanHeadImage[index]; } public void setCleaningHeadImage(int[] cleanHeadImage) { this.cleanHeadImage = cleanHeadImage; } public static int getHappinessHeadImage(int index) { return happinessHeadImage[index]; } public void setHappinessHeadImage(int[] happinessHeadImage) { this.happinessHeadImage = happinessHeadImage; } public static int getOnTouchImage(int index) { return onTouchImage[index]; } public void setOnTouchImage(int[] onTouchImage) { this.onTouchImage = onTouchImage; } }
然后,MainActivity及其布局文件
package com.ldgforever.NZpetty; import java.util.Map; import com.ldgforever.savedata.savedataBySharedPreference; import android.app.Activity; import android.app.AlarmManager; import android.app.AlertDialog; import android.app.PendingIntent; import android.app.AlertDialog.Builder; import android.content.Context; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.os.Handler; import android.preference.PreferenceManager; import android.view.View; import android.view.Window; import android.view.animation.Animation; import android.view.animation.Animation.AnimationListener; import android.view.animation.TranslateAnimation; import android.widget.ProgressBar; import android.widget.Toast; import pl.droidsonroids.gif.GifDrawable; import pl.droidsonroids.gif.GifImageView; public class MainActivity extends Activity{ private static ProgressBar mHungerProgress; private static ProgressBar mCleanProgress; private static ProgressBar mHappinessProgress; public static int mHunger; public static int mCleanness; public static int mHappiness; private int mRole; private String RoleChoosen = null; private GifImageView gifImageViewWW; private GifImageView gifImageViewWE; private GifImageView gifImageViewMovement; private GifDrawable gifDrawableWW; private GifDrawable gifDrawableWE; private GifDrawable gifDrawableMovement; private TranslateAnimation translateAnimationWE; private TranslateAnimation translateAnimationWW; private TranslateAnimation translateAnimationMM; private TranslateAnimation mHiddenAction; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); mHungerProgress = (ProgressBar) findViewById(R.id.progressHunger); mCleanProgress = (ProgressBar) findViewById(R.id.progressClean); mHappinessProgress = (ProgressBar) findViewById(R.id.progressPlay); initProgressBar(); gifImageViewWE = (GifImageView) findViewById(R.id.gif01); gifImageViewWW = (GifImageView) findViewById(R.id.gif02); gifImageViewMovement = (GifImageView) findViewById(R.id.gif03); /** * 回显数据 */ Map<String, String> userInfoMap = savedataBySharedPreference.getUserInfo(getApplicationContext()); if (userInfoMap != null) { RoleChoosen = userInfoMap.get("Role"); } /** * 角色选择对话框 */ if (RoleChoosen != null) { } else { AlertDialog.Builder builder = new Builder(this); builder.setTitle("请选择角色"); final String[] items = { "冥" }; builder.setSingleChoiceItems(items, 2, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(), "选择[角色]" + items[which], 0).show(); RoleChoosen = "冥"; savedataBySharedPreference.saveUserInfo(getApplicationContext(), RoleChoosen); mRole = which; } }); builder.setPositiveButton("确定", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.setCancelable(false); builder.create().show(); } SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); mHunger = preferences.getInt(MyIntentService.PREF_MYHUNGER, 100); mCleanness = preferences.getInt(MyIntentService.PREF_MYCLEAN, 100); mHappiness = preferences.getInt(MyIntentService.PREF_MYHAPPINESS, 100); updateProgressBar(mHunger,mCleanness ,mHappiness); /** * 后台服务 */ MyIntentService.startService(this,true); initAnimation(); LoadGifPic(); } /** * 喂食 * * @param view */ public void onFeed(View view) { double random = Math.random(); mHunger = petState.getHunger(); mHunger = mHunger + (int) (random * 25); petState.setHunger(mHunger); if ((random * 25) > 20) { // 20% showRandomDialog_Hunger(); } if (mHunger > 100) { mHunger = 100; } updateProgressBar(mHunger,mCleanness ,mHappiness); mHungerProgress.setProgress(mHunger); Toast.makeText(getApplicationContext(), "喂食成功", 0).show(); } /** * 清洁 * * @param view */ public void onClean(View view) { double random = Math.random(); mCleanness = petState.getCleaning(); mCleanness = mCleanness + (int) (random * 15); petState.setCleaning(mCleanness); if ((random * 15) > 10) { // 33.3% showRandomDialog_Cleaning(); } if (mCleanness > 100) { mCleanness = 100; } updateProgressBar(mHunger,mCleanness ,mHappiness); mCleanProgress.setProgress(mCleanness); Toast.makeText(getApplicationContext(), "清洁成功", 0).show(); } /** * 玩耍 * * @param view */ public void onPlay(View view) { double random = Math.random(); mHappiness = petState.getHappiness(); mHappiness = mHappiness + (int) (random * 20); petState.setHappiness(mHappiness); if ((random * 20) > 15) { // 25% showRandomDialog_Happiness(); } else { PlayAnimation(); } if (mHappiness > 100) { mHappiness = 100; } updateProgressBar(mHunger,mCleanness ,mHappiness); mHappinessProgress.setProgress(mHappiness); } /** * 更新进度条 */ public static void setProgress(int mHunger, int mCleanness, int mHappiness) { mHungerProgress.setProgress(mHunger); mCleanProgress.setProgress(mCleanness); mHappinessProgress.setProgress(mHappiness); } /** * 更新宠物当前状态 */ public static void getState() { mHunger = petState.getHunger(); mCleanness = petState.getCleaning(); mHappiness = petState.getHappiness(); } /** * 存储宠物当前状态 */ public static void setState(int mHunger,int mCleanness,int mHappiness) { petState.setHunger(mHunger); petState.setCleaning(mCleanness); petState.setHappiness(mHappiness); } /** * 来回行走的动画 */ private void AnimTranslate() { gifImageViewWE.setVisibility(GifImageView.VISIBLE); gifImageViewWW.setVisibility(GifImageView.INVISIBLE); translateAnimationWE.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationRepeat(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { gifImageViewWE.setVisibility(GifImageView.INVISIBLE); gifImageViewWW.setVisibility(GifImageView.VISIBLE); gifImageViewWW.setAnimation(translateAnimationWW); gifImageViewWW.startAnimation(translateAnimationWW); } }); translateAnimationWW.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationRepeat(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { gifImageViewWE.setVisibility(GifImageView.VISIBLE); gifImageViewWW.setVisibility(GifImageView.INVISIBLE); gifImageViewWE.setAnimation(translateAnimationWE); gifImageViewWE.startAnimation(translateAnimationWE); } }); gifImageViewWE.startAnimation(translateAnimationWE); } /** * 加载gif动画 */ private void LoadGifPic() { try { // 如果加载的是gif动图,第一步需要先将gif动图资源转化为GifDrawable // 将gif图资源转化为GifDrawable gifDrawableWW = new GifDrawable(getResources(), R.drawable.mingww); gifDrawableWE = new GifDrawable(getResources(), R.drawable.mingwe); // gif1加载一个动态图gif gifImageViewWE.setImageDrawable(gifDrawableWE); gifImageViewWW.setImageDrawable(gifDrawableWW); AnimTranslate(); } catch (Exception e) { e.printStackTrace(); } } /** * 打开随机对话框 */ private void showRandomDialog_Hunger() { Intent i = new Intent(MainActivity.this, RandomDialogHunger.class); startActivity(i); } /** * 打开随机对话框 */ private void showRandomDialog_Cleaning() { Intent i = new Intent(MainActivity.this, RandomDialogCleaning.class); startActivity(i); } /** * 打开随机对话框 */ private void showRandomDialog_Happiness() { Intent i = new Intent(MainActivity.this, RandomDialogHappiness.class); startActivity(i); } /** * 玩耍的随机动画 */ private void PlayAnimation() { try { int Random = (int) (Math.random() * 5.99); gifDrawableMovement = new GifDrawable(getResources(), petState.getOnTouchImage(Random)); gifImageViewMovement.setImageDrawable(gifDrawableMovement); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } gifImageViewWE.startAnimation(mHiddenAction); gifImageViewWW.startAnimation(mHiddenAction); gifImageViewWE.setVisibility(GifImageView.INVISIBLE); gifImageViewWW.setVisibility(GifImageView.INVISIBLE); gifImageViewMovement.startAnimation(translateAnimationMM); translateAnimationMM.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) { // TODO Auto-generated method stub gifImageViewMovement.setVisibility(GifImageView.VISIBLE); } @Override public void onAnimationRepeat(Animation animation) { // TODO Auto-generated method stub } @Override public void onAnimationEnd(Animation animation) { // TODO Auto-generated method stub gifImageViewMovement.startAnimation(mHiddenAction); gifImageViewMovement.setVisibility(GifImageView.INVISIBLE); AnimTranslate(); } }); } /** * 动画初始化 */ private void initAnimation() { translateAnimationWE = new TranslateAnimation(TranslateAnimation.RELATIVE_TO_PARENT, 0, TranslateAnimation.RELATIVE_TO_PARENT, 0.9f, TranslateAnimation.RELATIVE_TO_PARENT, 0, TranslateAnimation.RELATIVE_TO_PARENT, -0.1f); translateAnimationWE.setDuration(10000); translateAnimationWW = new TranslateAnimation(TranslateAnimation.RELATIVE_TO_PARENT, 0f, TranslateAnimation.RELATIVE_TO_PARENT, -0.9f, TranslateAnimation.RELATIVE_TO_PARENT, 0, TranslateAnimation.RELATIVE_TO_PARENT, -0.2f); translateAnimationWW.setDuration(10000); translateAnimationMM = new TranslateAnimation(TranslateAnimation.RELATIVE_TO_PARENT, 0, TranslateAnimation.RELATIVE_TO_PARENT, 0, TranslateAnimation.RELATIVE_TO_PARENT, 0, TranslateAnimation.RELATIVE_TO_PARENT, 0); translateAnimationMM.setDuration(1000); mHiddenAction = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 2.0f); mHiddenAction.setDuration(2000); } public static void updateProgressBar(int mHunger,int mCleanness ,int mHappiness) { mHunger = petState.getHunger(); mCleanness = petState.getCleaning(); mHappiness = petState.getHappiness(); setState(mHunger,mCleanness,mHappiness); setProgress(mHunger, mCleanness, mHappiness); } private void initProgressBar() { mHungerProgress.setMax(100); //mHungerProgress.setProgress(100); mCleanProgress.setMax(100); //mCleanProgress.setProgress(100); mHappinessProgress.setMax(100); //mHappinessProgress.setProgress(100); } }
<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:background="@drawable/nzbg" tools:context="com.ldgforever.NZpetty.MainActivity" > <Button android:id="@+id/feed" android:onClick="onFeed" android:layout_width="wrap_content" android:layout_height="30dp" android:layout_margin="5dp" android:background="#FA8072" android:textColor="#FFFFFF" android:text="喂食" /> <Button android:id="@+id/clean" android:layout_below="@id/feed" android:onClick="onClean" android:layout_width="wrap_content" android:layout_height="30dp" android:background="#FA8072" android:textColor="#FFFFFF" android:layout_margin="5dp" android:text="清洁" /> <Button android:id="@+id/play" android:layout_below="@id/clean" android:onClick="onPlay" android:layout_width="wrap_content" android:layout_height="30dp" android:background="#FA8072" android:textColor="#FFFFFF" android:layout_margin="5dp" android:text="玩耍" /> <ProgressBar android:id="@+id/progressHunger" style="?android:attr/progressBarStyleHorizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignTop="@+id/feed" android:layout_toRightOf="@+id/feed" /> <ProgressBar android:id="@+id/progressClean" style="?android:attr/progressBarStyleHorizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignTop="@+id/clean" android:layout_toRightOf="@+id/clean" /> <ProgressBar android:id="@+id/progressPlay" style="?android:attr/progressBarStyleHorizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignTop="@+id/play" android:layout_toRightOf="@+id/play" /> <pl.droidsonroids.gif.GifImageView android:id="@+id/gif01" android:layout_width="60dp" android:layout_height="140dp" android:enabled="false" android:layout_alignParentBottom="true" /> <pl.droidsonroids.gif.GifImageView android:id="@+id/gif02" android:layout_width="60dp" android:layout_height="140dp" android:enabled="false" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" /> <pl.droidsonroids.gif.GifImageView android:id="@+id/gif03" android:layout_width="80dp" android:layout_height="120dp" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:enabled="false" /> </RelativeLayout>
再然后,后台更新数据的IntentService,每15min更新一次状态并从当前状态中扣除一个有范围的随机数来模拟宠物的消耗,周期定时采用的是AlarmManager。
package com.ldgforever.NZpetty; import android.app.AlarmManager; import android.app.IntentService; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.preference.PreferenceManager; import android.support.v4.os.ResultReceiver; import android.util.Log; public class MyIntentService extends IntentService { private static final String TAG = "MyIntentService"; private int mHunger; private int mClean; private int mHappiness; private static final int POLL_INTERVAL = 1000*60*15; public static final String PREF_IS_ALARM_ON = "isAlarmOn"; public static final String PREF_MYHUNGER = "mHunger"; public static final String PREF_MYCLEAN = "mClean"; public static final String PREF_MYHAPPINESS = "mHappiness"; public MyIntentService() { super(TAG); } @Override protected void onHandleIntent(Intent intent) { Log.i(TAG, "onHandleIntent"); double random = Math.random(); mHunger = petState.getHunger() - (int) (20 * random); if (mHunger > 100) { mHunger = 100; } if (mHunger < 0) { mHunger = 0; } petState.setHunger(mHunger); mClean = petState.getCleaning() - (int) (10 * random); if (mClean > 100) { mClean = 100; } if (mClean < 0) { mClean = 0; } petState.setHunger(mClean); mHappiness = petState.getHappiness() - (int) (15 * random); if (mHappiness > 100) { mHappiness = 100; } if (mHappiness < 0) { mHappiness = 0; } PreferenceManager.getDefaultSharedPreferences(getApplicationContext()) .edit() .putInt(PREF_MYHUNGER, mHunger) .putInt(PREF_MYCLEAN, mClean) .putInt(PREF_MYHAPPINESS, mHappiness) .commit(); MainActivity.setState(mHunger,mClean,mHappiness); MainActivity.updateProgressBar(mHunger,mClean,mHappiness); /* try { Thread.sleep(1000*60*15); } catch (InterruptedException e1) { e1.printStackTrace(); } */ } public static void startService(Context context , boolean isOn) { Intent intent = new Intent(context, MyIntentService.class); PendingIntent pi = PendingIntent.getService(context, 0, intent, 0); AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); if(isOn){ alarmManager.setRepeating(AlarmManager.RTC, System.currentTimeMillis(), POLL_INTERVAL, pi); }else{ alarmManager.cancel(pi); pi.cancel(); } PreferenceManager.getDefaultSharedPreferences(context) .edit() .putBoolean(MyIntentService.PREF_IS_ALARM_ON, isOn) .commit(); //context.startService(intent); } }
并且,为了能在手机重启时启动定时器,这里使用了广播,新建了一个StartUpReceiver,并用SharePreference存储一个布尔类型的状态。清单文件在后面放出。
package com.ldgforever.NZpetty; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.preference.PreferenceManager; public class StartupReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); boolean isOn = preferences.getBoolean(MyIntentService.PREF_IS_ALARM_ON, false); MyIntentService.startService(context,isOn); } }
储存当前角色信息也用的是SharePreference,但目前只做了一个角色所以是没什么用处的,也贴上来
package com.ldgforever.savedata; import java.util.HashMap; import java.util.Map; import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.text.TextUtils; public class savedataBySharedPreference { public static boolean saveUserInfo(Context context, String RoleChoosen){ try { SharedPreferences sp = context.getSharedPreferences("Role", context.MODE_WORLD_READABLE); Editor editor = sp.edit(); editor.putString("Role", RoleChoosen); editor.commit(); //存储数据 return true; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return false; } public static Map<String, String> getUserInfo(Context context){ SharedPreferences sp = context.getSharedPreferences("Role", context.MODE_PRIVATE); String RoleChoosen = sp.getString("Role", null); if(!TextUtils.isEmpty(RoleChoosen)){ Map<String, String> userInfoMap = new HashMap<String, String>(); userInfoMap.put("Role", RoleChoosen); return userInfoMap; } return null; } }
接下来,是三个类用以显示喂食,清洁和玩耍时出现的内容相对随机的对话框Activity和他们对应的布局文件。
1.喂食
package com.ldgforever.NZpetty; import android.app.Activity; import android.os.Bundle; import android.view.Gravity; import android.view.Window; import android.view.WindowManager; import android.widget.TextView; import pl.droidsonroids.gif.GifDrawable; import pl.droidsonroids.gif.GifImageView; public class RandomDialogHunger extends Activity { private GifImageView gifImageViewHead; private GifDrawable gifDrawableHead; private TextView mDialogText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); Window win = this.getWindow(); win.getDecorView().setPadding(0, 0, 0, 0); WindowManager.LayoutParams lp = win.getAttributes(); lp.width = WindowManager.LayoutParams.MATCH_PARENT; lp.height = WindowManager.LayoutParams.WRAP_CONTENT; lp.gravity = Gravity.CENTER; win.setAttributes(lp); setContentView(R.layout.random_dialog_hunger); gifImageViewHead = (GifImageView) findViewById(R.id.gifhead01); try { int Random = (int) (Math.random() * 7.99); gifDrawableHead = new GifDrawable(getResources(), petState.getHungerHeadImage(Random)); gifImageViewHead.setImageDrawable(gifDrawableHead); mDialogText = (TextView)findViewById(R.id.mdialogtext01); mDialogText.setText(petState.getHungerDialog(Random)); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#e0F0B0B0"> <pl.droidsonroids.gif.GifImageView android:id="@+id/gifhead01" android:layout_width="120dp" android:layout_height="120dp" android:enabled="false"/> <TextView android:id="@+id/mdialogtext01" android:layout_width="wrap_content" android:layout_height="80dp" android:layout_alignParentRight="true" android:layout_marginTop="20dp" android:layout_toRightOf="@+id/gifhead01" android:textColor="#FFFFFF" /> </RelativeLayout>
2.清洁
package com.ldgforever.NZpetty; import android.app.Activity; import android.os.Bundle; import android.view.Gravity; import android.view.Window; import android.view.WindowManager; import android.widget.TextView; import pl.droidsonroids.gif.GifDrawable; import pl.droidsonroids.gif.GifImageView; public class RandomDialogCleaning extends Activity { private GifImageView gifImageViewHead; private GifDrawable gifDrawableHead; private TextView mDialogText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); Window win = this.getWindow(); win.getDecorView().setPadding(0, 0, 0, 0); WindowManager.LayoutParams lp = win.getAttributes(); lp.width = WindowManager.LayoutParams.MATCH_PARENT; lp.height = WindowManager.LayoutParams.WRAP_CONTENT; lp.gravity = Gravity.CENTER; win.setAttributes(lp); setContentView(R.layout.random_dialog_cleaning); gifImageViewHead = (GifImageView) findViewById(R.id.gifhead02); try { int Random = (int) (Math.random() * 5.99); gifDrawableHead = new GifDrawable(getResources(), petState.getCleaningHeadImage(Random)); gifImageViewHead.setImageDrawable(gifDrawableHead); mDialogText = (TextView)findViewById(R.id.mdialogtext02); mDialogText.setText(petState.getCleanDialog(Random)); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#e0AEEEEE"> <pl.droidsonroids.gif.GifImageView android:id="@+id/gifhead02" android:layout_width="120dp" android:layout_height="120dp" android:enabled="false"/> <TextView android:id="@+id/mdialogtext02" android:layout_width="wrap_content" android:layout_height="80dp" android:layout_alignParentRight="true" android:layout_marginTop="20dp" android:layout_toRightOf="@+id/gifhead02" android:textColor="#FFFFFF" /> </RelativeLayout>
3.玩耍
package com.ldgforever.NZpetty; import android.app.Activity; import android.os.Bundle; import android.view.Gravity; import android.view.Window; import android.view.WindowManager; import android.widget.TextView; import pl.droidsonroids.gif.GifDrawable; import pl.droidsonroids.gif.GifImageView; public class RandomDialogHappiness extends Activity { private GifImageView gifImageViewHead; private GifDrawable gifDrawableHead; private TextView mDialogText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); Window win = this.getWindow(); win.getDecorView().setPadding(0, 0, 0, 0); WindowManager.LayoutParams lp = win.getAttributes(); lp.width = WindowManager.LayoutParams.MATCH_PARENT; lp.height = WindowManager.LayoutParams.WRAP_CONTENT; lp.gravity = Gravity.CENTER; win.setAttributes(lp); setContentView(R.layout.random_dialog_happiness); gifImageViewHead = (GifImageView) findViewById(R.id.gifhead03); try { int Random = (int) (Math.random() * 9.99); gifDrawableHead = new GifDrawable(getResources(), petState.getHappinessHeadImage(Random)); gifImageViewHead.setImageDrawable(gifDrawableHead); mDialogText = (TextView)findViewById(R.id.mdialogtext03); mDialogText.setText(petState.getPlayDialog(Random)); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#e0FFF8DC"> <pl.droidsonroids.gif.GifImageView android:id="@+id/gifhead03" android:layout_width="120dp" android:layout_height="120dp" android:enabled="false"/> <TextView android:id="@+id/mdialogtext03" android:layout_width="wrap_content" android:layout_height="80dp" android:layout_alignParentRight="true" android:layout_marginTop="20dp" android:layout_toRightOf="@+id/gifhead03" android:textColor="#FFFFFF" /> </RelativeLayout>
清单文件:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.ldgforever.NZpetty" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="23" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:screenOrientation="landscape" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".RandomDialogHunger" android:screenOrientation="landscape" android:label="@string/app_name" android:theme="@android:style/Theme.Dialog"></activity> <activity android:name=".RandomDialogCleaning" android:screenOrientation="landscape" android:label="@string/app_name" android:theme="@android:style/Theme.Dialog"></activity> <activity android:name=".RandomDialogHappiness" android:screenOrientation="landscape" android:label="@string/app_name" android:theme="@android:style/Theme.Dialog"></activity> <service android:name="com.ldgforever.NZpetty.MyIntentService"></service> <receiver android:name="com.ldgforever.NZpetty.StartupReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"/> </intent-filter> </receiver>" </application> </manifest>
标签:
原文地址:http://www.cnblogs.com/ldgforever/p/5854245.html