您的位置:首页 > 移动开发

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

2013-10-11 22:18 155 查看
http://berdy.iteye.com/blog/1768905

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

Java代码  


import android.app.Activity;  

import android.content.Intent;  

import android.os.Bundle;  

import android.widget.ImageView;  

  

public class SplashActivity extends Activity {  

  

    @Override  

    protected void onCreate(Bundle savedInstanceState) {  

        super.onCreate(savedInstanceState);  

        setContentView(R.layout.splash);  

  

        ImageView splashImg = (ImageView) findViewById(R.id.splash_image);  

        splashImg.postDelayed(new Runnable() {//这里利用了View的postDelayed  

  

            public void run() {  

                Intent intent = new Intent();  

                intent.setClass(SplashActivity.this, MainActivity.class);  

                startActivity(intent);  

                finish();  

            }  

        }, 1000);  

    }  

}  

下面是splash.xml,layout 文件了 

Xml代码  


<?xml version="1.0" encoding="utf-8"?>  

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  

    android:layout_width="fill_parent"  

    android:layout_height="fill_parent" >  

  

    <ImageView  

        android:id="@+id/splash_background"  

        android:layout_width="fill_parent"  

        android:layout_height="fill_parent"  

        android:layout_alignParentTop="true"  

        android:layout_centerHorizontal="true"  

        android:scaleType="fitXY"  

        android:src="@drawable/splash_floor" />  

  

    <ImageView  

        android:id="@+id/splash_image"  

        android:layout_width="fill_parent"  

        android:layout_height="wrap_content"  

        android:layout_alignParentTop="true"  

        android:layout_centerHorizontal="true"  

        android:scaleType="fitXY"  

        android:src="@drawable/splash_logo" />  

  

    <ImageView  

        android:id="@+id/splash_foot"  

        android:layout_width="wrap_content"  

        android:layout_height="wrap_content"  

        android:layout_alignParentBottom="true"  

        android:layout_centerHorizontal="true"  

        android:layout_marginBottom="10.0dip"  

        android:src="@drawable/splash_logo_foot" />  

  

</RelativeLayout>  

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

Java代码  


package com.upon.common.view;  

  

import android.content.Context;  

import android.content.res.TypedArray;  

import android.graphics.Canvas;  

import android.util.AttributeSet;  

import android.widget.ImageView;  

  

import com.upon.xxxx.R;  

  

public class UponRotateImageView extends ImageView {  

  

    private int mAngle;  

  

    public UponRotateImageView(Context context, AttributeSet attrs, int defStyle) {  

        super(context, attrs, defStyle);  

        loadAttributes(context, attrs);  

    }  

  

    public UponRotateImageView(Context context, AttributeSet attrs) {  

        super(context, attrs);  

        loadAttributes(context, attrs);  

    }  

  

    public UponRotateImageView(Context context) {  

        super(context);  

    }  

  

    private void loadAttributes(Context context, AttributeSet attrs) {  

        TypedArray arr = context.obtainStyledAttributes(attrs, R.styleable.RotateImageView);  

        mAngle = arr.getInteger(R.styleable.RotateImageView_angle, 0);  

        arr.recycle();  

    }  

  

    public int getAngle() {  

        return mAngle;  

    }  

  

    public void setAngle(int angle) {  

        mAngle = angle;  

    }  

  

    @Override  

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  

        int w = getDrawable().getIntrinsicWidth();  

        int h = getDrawable().getIntrinsicHeight();  

        double a = Math.toRadians(mAngle);  

  

        int width = (int) (Math.abs(w * Math.cos(a)) + Math.abs(h * Math.sin(a)));  

        int height = (int) (Math.abs(w * Math.sin(a)) + Math.abs(h * Math.cos(a)));  

  

        setMeasuredDimension(width, height);  

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);  

    }  

  

    @Override  

    protected void onDraw(Canvas canvas) {  

        canvas.save();  

        canvas.rotate(mAngle % 360, getWidth() / 2, getHeight() / 2);  

        getDrawable().draw(canvas);  

        canvas.restore();  

    }  

}  

attrs.xm文件中增加angle属性 

Xml代码  


<?xml version="1.0" encoding="utf-8"?>  

<resources>  

    <declare-styleable name="RotateImageView">  

        <attr name="angle" format="integer" />  

    </declare-styleable>  

  

</resources>  

使用UponRotateImageView 

Xml代码  


<?xml version="1.0" encoding="utf-8"?>  

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  

    xmlns:upon="http://schemas.android.com/apk/res/com.upon.xxxx"  

    android:layout_width="wrap_content"  

    android:layout_height="wrap_content" >  

  

    <com.upon.common.view.UponRotateImageView  

        android:id="@+id/bkg_img"  

        android:layout_width="wrap_content"  

        android:layout_height="wrap_content"  

        android:src="@drawable/conquer_nation_bkg"  

        upon:angle="45" />  

  

</RelativeLayout>  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐