编写App的开场Activity 和 扩展ImageView使可旋转

来源:互联网 发布:阿里云域名管理登录 编辑:程序博客网 时间:2024/06/05 17:37

http://berdy.iteye.com/blog/1768905


在android的app和游戏的应用中,都会有个开场场景,老外管这个叫splash。 
现在就编写个简单的SplashActivity 

Java代码  收藏代码
  1. import android.app.Activity;  
  2. import android.content.Intent;  
  3. import android.os.Bundle;  
  4. import android.widget.ImageView;  
  5.   
  6. public class SplashActivity extends Activity {  
  7.   
  8.     @Override  
  9.     protected void onCreate(Bundle savedInstanceState) {  
  10.         super.onCreate(savedInstanceState);  
  11.         setContentView(R.layout.splash);  
  12.   
  13.         ImageView splashImg = (ImageView) findViewById(R.id.splash_image);  
  14.         splashImg.postDelayed(new Runnable() {//这里利用了View的postDelayed  
  15.   
  16.             public void run() {  
  17.                 Intent intent = new Intent();  
  18.                 intent.setClass(SplashActivity.this, MainActivity.class);  
  19.                 startActivity(intent);  
  20.                 finish();  
  21.             }  
  22.         }, 1000);  
  23.     }  
  24. }  


下面是splash.xml,layout 文件了 
Xml代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent" >  
  5.   
  6.     <ImageView  
  7.         android:id="@+id/splash_background"  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="fill_parent"  
  10.         android:layout_alignParentTop="true"  
  11.         android:layout_centerHorizontal="true"  
  12.         android:scaleType="fitXY"  
  13.         android:src="@drawable/splash_floor" />  
  14.   
  15.     <ImageView  
  16.         android:id="@+id/splash_image"  
  17.         android:layout_width="fill_parent"  
  18.         android:layout_height="wrap_content"  
  19.         android:layout_alignParentTop="true"  
  20.         android:layout_centerHorizontal="true"  
  21.         android:scaleType="fitXY"  
  22.         android:src="@drawable/splash_logo" />  
  23.   
  24.     <ImageView  
  25.         android:id="@+id/splash_foot"  
  26.         android:layout_width="wrap_content"  
  27.         android:layout_height="wrap_content"  
  28.         android:layout_alignParentBottom="true"  
  29.         android:layout_centerHorizontal="true"  
  30.         android:layout_marginBottom="10.0dip"  
  31.         android:src="@drawable/splash_logo_foot" />  
  32.   
  33. </RelativeLayout>  


继承ImageView,增加angle属性,重写OnMeasure和OnDraw方法 

Java代码  收藏代码
  1. package com.upon.common.view;  
  2.   
  3. import android.content.Context;  
  4. import android.content.res.TypedArray;  
  5. import android.graphics.Canvas;  
  6. import android.util.AttributeSet;  
  7. import android.widget.ImageView;  
  8.   
  9. import com.upon.xxxx.R;  
  10.   
  11. public class UponRotateImageView extends ImageView {  
  12.   
  13.     private int mAngle;  
  14.   
  15.     public UponRotateImageView(Context context, AttributeSet attrs, int defStyle) {  
  16.         super(context, attrs, defStyle);  
  17.         loadAttributes(context, attrs);  
  18.     }  
  19.   
  20.     public UponRotateImageView(Context context, AttributeSet attrs) {  
  21.         super(context, attrs);  
  22.         loadAttributes(context, attrs);  
  23.     }  
  24.   
  25.     public UponRotateImageView(Context context) {  
  26.         super(context);  
  27.     }  
  28.   
  29.     private void loadAttributes(Context context, AttributeSet attrs) {  
  30.         TypedArray arr = context.obtainStyledAttributes(attrs, R.styleable.RotateImageView);  
  31.         mAngle = arr.getInteger(R.styleable.RotateImageView_angle, 0);  
  32.         arr.recycle();  
  33.     }  
  34.   
  35.     public int getAngle() {  
  36.         return mAngle;  
  37.     }  
  38.   
  39.     public void setAngle(int angle) {  
  40.         mAngle = angle;  
  41.     }  
  42.   
  43.     @Override  
  44.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  45.         int w = getDrawable().getIntrinsicWidth();  
  46.         int h = getDrawable().getIntrinsicHeight();  
  47.         double a = Math.toRadians(mAngle);  
  48.   
  49.         int width = (int) (Math.abs(w * Math.cos(a)) + Math.abs(h * Math.sin(a)));  
  50.         int height = (int) (Math.abs(w * Math.sin(a)) + Math.abs(h * Math.cos(a)));  
  51.   
  52.         setMeasuredDimension(width, height);  
  53.         super.onMeasure(widthMeasureSpec, heightMeasureSpec);  
  54.     }  
  55.   
  56.     @Override  
  57.     protected void onDraw(Canvas canvas) {  
  58.         canvas.save();  
  59.         canvas.rotate(mAngle % 360, getWidth() / 2, getHeight() / 2);  
  60.         getDrawable().draw(canvas);  
  61.         canvas.restore();  
  62.     }  
  63. }  

attrs.xm文件中增加angle属性 

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <declare-styleable name="RotateImageView">  
  4.         <attr name="angle" format="integer" />  
  5.     </declare-styleable>  
  6.   
  7. </resources>  


使用UponRotateImageView 
Xml代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:upon="http://schemas.android.com/apk/res/com.upon.xxxx"  
  4.     android:layout_width="wrap_content"  
  5.     android:layout_height="wrap_content" >  
  6.   
  7.     <com.upon.common.view.UponRotateImageView  
  8.         android:id="@+id/bkg_img"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:src="@drawable/conquer_nation_bkg"  
  12.         upon:angle="45" />  
  13.   
  14. </RelativeLayout>