关闭

Android自学笔记(四)简单电子宠物的实现

标签: android电子宠物逆转裁判gif
696人阅读 评论(0) 收藏 举报
分类:

 由于本人还是个菜鸡……代码质量难以保证,so仅供个人学习记录目的和仅供参考……要是发现什么bug会编辑博客……有意见建议欢迎提出!

  最近在Android学习的过程中接触到了简单的动画效果,就突然想做个电子宠物应用一下(~ ̄▽ ̄)~*。

  素材来源是游戏逆转裁判的素材……也算表达一下个人对这一系列的爱……(~ ̄▽ ̄)~*。

gif的显示用的是GitHub的开源库:android-gif-drawable,https://github.com/koral--/android-gif-drawable

实现之后的界面如下,左上角有喂食,清洁和玩耍三个选项,屏幕下方是角色活动区域,由于个人水平不够,目前只能按照规定的路线移动。然后在按钮右侧,采用了ProgressBar来显示角色当前的状态,状态由service后台更新。


  首先,定义了一个存储状态,资源文件id,对话等的类,如下

package com.ldgforever.NZpetty;

import com.ldgforever.NZpetty.R.drawable;

public class petState {
	
	private static int mHunger = 100;
	private static int mCleaning = 100;
	private static int 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及其布局文件

<pre class="java" name="code">package com.ldgforever.NZpetty;

import java.util.Map;
import com.ldgforever.savedata.savedataBySharedPreference;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
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);
		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();
		}
		
		/**
		 * 后台服务
		 */
		Intent intent = new Intent(getApplicationContext(), MyService.class);
		startService(intent);
		
		initAnimation();
		LoadGifPic();	
	}

	/**
	 * 喂食
	 * @param view
	 */
	public void onFeed(View view) {
		double random = Math.random();
		mHunger = mHunger + (int) (random * 25);
		if ((random * 25) > 20) {  //20%
			showRandomDialog_Hunger();
		}
		if(mHunger>100){
			mHunger = 100;
		}
		setState();
		mHungerProgress.setProgress(mHunger);
		Toast.makeText(getApplicationContext(), "喂食成功", 0).show();
	}

	/**
	 * 清洁
	 * @param view
	 */
	public void onClean(View view) {
		double random = Math.random();
		mCleanness = mCleanness + (int) (random * 15);
		if ((random * 15) > 10) {   //33.3%
			showRandomDialog_Cleaning();
		}
		if(mCleanness>100){
			mCleanness = 100;
		}
		setState();
		mCleanProgress.setProgress(mCleanness);
		Toast.makeText(getApplicationContext(), "清洁成功", 0).show();
	}

	/**
	 * 玩耍
	 * @param view
	 */
	public void onPlay(View view) {
		double random = Math.random();
		mHappiness = mHappiness + (int) (random * 20);
		if ((random * 20) > 15) {   //25%
			showRandomDialog_Happiness();
		}else{
			PlayAnimation();
		}
		if(mHappiness>100){
			mHappiness = 100;
		}
		setState();
		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() {
		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);
	}
}



<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>

  再然后,后台更新数据的service,每一小时更新一次状态并从当前状态中扣除一个有范围的随机数来模拟宠物的消耗。

<pre class="java" name="code">package com.ldgforever.NZpetty;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.widget.Toast;

public class MyService extends Service {

	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}

	@Override
	public boolean onUnbind(Intent intent) {
		return super.onUnbind(intent);
	}

	@Override
	public void onCreate() {
		System.out.println("onCreate");
		super.onCreate();
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		Timer mTimer = new Timer();
		TimerTask mTimerTask = new TimerTask() {
			private int mHunger;
			private int mClean;
			private int mHappiness;

			@Override
			public void run() {
				// TODO Auto-generated method stub
				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;
				}
				petState.setHunger(mHappiness);

				MainActivity.setProgress(mHunger, mClean, mHappiness);
			}
		};
		mTimer.schedule(mTimerTask, 0, 900000);
		return super.onStartCommand(intent, flags, startId);
	}

	@Override
	public void onDestroy() {
		System.out.println("onDestory");
		super.onDestroy();
	}

}



  储存当前角色信息用的是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>

完成之后效果如下





1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:14620次
    • 积分:134
    • 等级:
    • 排名:千里之外
    • 原创:20篇
    • 转载:0篇
    • 译文:0篇
    • 评论:6条
    文章分类