編輯:Android開發實例
效果圖:
創建Toast類
- package com.example.messageboxtest;
- import android.app.Activity;
- import android.content.Context;
- import android.os.Handler;
- import android.view.Gravity;
- import android.view.View;
- import android.view.ViewGroup;
- import android.view.animation.AlphaAnimation;
- import android.view.animation.Animation;
- import android.widget.LinearLayout;
- import android.widget.TextView;
- /**
- *
- * @author chaowen
- *
- */
- public class MyMsgBox {
- private static final int ANIMATION_DURATION = 600;
- private int HIDE_DELAY = 5000;
- private View mContainer;
- private int gravity = Gravity.CENTER;
- private TextView mTextView;
- private Handler mHandler;
- private AlphaAnimation mFadeInAnimation;
- private AlphaAnimation mFadeOutAnimation;
- public MyMsgBox(Context context, int HIDE_DELAY, int gravity) {
- ViewGroup container = (ViewGroup) ((Activity) context)
- .findViewById(android.R.id.content);
- View v = ((Activity) context).getLayoutInflater().inflate(
- R.layout.newmb__messagebar, container);
- this.HIDE_DELAY = HIDE_DELAY;
- this.gravity = gravity;
- init(v);
- }
- private void init(View v) {
- mContainer = v.findViewById(R.id.mbContainer);
- mContainer.setVisibility(View.GONE);
- mTextView = (TextView) v.findViewById(R.id.mbMessage);
- mFadeInAnimation = new AlphaAnimation(0.0f, 1.0f);
- mFadeOutAnimation = new AlphaAnimation(1.0f, 0.0f);
- mFadeOutAnimation.setDuration(ANIMATION_DURATION);
- mFadeOutAnimation
- .setAnimationListener(new Animation.AnimationListener() {
- @Override
- public void onAnimationStart(Animation animation) {
- }
- @Override
- public void onAnimationEnd(Animation animation) {
- mContainer.setVisibility(View.GONE);
- }
- @Override
- public void onAnimationRepeat(Animation animation) {
- }
- });
- mHandler = new Handler();
- }
- public void show(String message) {
- mContainer.setVisibility(View.VISIBLE);
- ((LinearLayout) mContainer).setGravity(gravity
- | Gravity.CENTER_VERTICAL);
- mTextView.setText(message);
- mFadeInAnimation.setDuration(ANIMATION_DURATION);
- mContainer.startAnimation(mFadeInAnimation);
- mHandler.postDelayed(mHideRunnable, HIDE_DELAY);
- }
- private final Runnable mHideRunnable = new Runnable() {
- @Override
- public void run() {
- mContainer.startAnimation(mFadeOutAnimation);
- }
- };
- }
對應的布局
- ?<?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/mbContainer"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_margin="10dp"
- android:gravity="bottom"
- android:orientation="vertical" >
- <LinearLayout
- style="@style/bgTheme"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_margin="10dp"
- android:gravity="bottom"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/mbMessage"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Test"
- android:textColor="@drawable/white" />
- </LinearLayout>
- </LinearLayout>
使用方法:
- MyMsgBox m = new MyMsgBox(arg0.getContext(), 5000,
- Gravity.BOTTOM);
- m.show("這是一個性化Toast");
可以顯示在的Android任務,通過加載進度條的進展。進度條有兩種形狀。加載欄和加載微調(spinner)。在本章中,我們將討論微調(spinner)。Spinner 用
Android提供了許多方法來控制播放的音頻/視頻文件和流。其中該方法是通過一類稱為MediaPlayer。Android是提供MediaPlayer類訪問內置的媒體播放
在項目開發中,可能系統自帶的一些widget不能滿足我們的需求,這時就需要自定義View。 通過查看系統中的常用widget如Button,TextView,Ed
本文開始將為大家剖析Android示例程序-Snake貪吃蛇。貪吃蛇游戲大部分