编写App的开场Activity 和 扩展ImageView使可旋转
来源:互联网 发布:阿里云域名管理登录 编辑:程序博客网 时间:2024/06/05 17:37
http://berdy.iteye.com/blog/1768905
在android的app和游戏的应用中,都会有个开场场景,老外管这个叫splash。
现在就编写个简单的SplashActivity
- 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 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方法
- 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 version="1.0" encoding="utf-8"?>
- <resources>
- <declare-styleable name="RotateImageView">
- <attr name="angle" format="integer" />
- </declare-styleable>
- </resources>
使用UponRotateImageView
- <?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>
- 编写App的开场Activity 和 扩展ImageView使可旋转
- Android 可平移,缩放,旋转的ImageView
- 自定义ImageView,点击可旋转
- 可平移缩放旋转的ImageView的实现
- 旋转ImageView和TextView的效果实现
- 编写可接收浏览Intent的Activity
- app首次启动的开场动画
- Android App开场图片动画的实现
- RotateImageView 旋转的ImageView
- 使用 Jetty 和 Direct Web Remoting 编写可扩展的 Comet 应用程序
- 可移动的ImageView
- 可缩放的ImageView
- android自定义控件:可旋转View:可作为ImageView、ImageButton
- android自定义控件:可旋转View:可作为ImageView、ImageButton
- ImageView实现图像的放大缩小和旋转功能
- Android----imageView实现图片的旋转和缩放
- ImageView实现对图片的缩放和旋转
- 如何开始一个模块化可扩展的Web App
- 【leetcode】Pow(x, n)
- [Google] RMQ 区间最值问题
- HTTP代理实现请求报文的拦截与篡改2--功能介绍+源码下载
- 与IO相关的等待事件troubleshooting-系列8
- php study 4 array
- 编写App的开场Activity 和 扩展ImageView使可旋转
- oracle ebs Customer Info
- 【js学习笔记-054】标准转换方法
- poj1852 蚂蚁
- mysql过滤数据(一)
- OpenStack安装部署管理中常见问题解决方法(OpenStack-Lite-FAQ)
- 机器学习书籍论文推荐
- 输出从n个数中选m个数的所有组合
- Cocos2d-x 3.0开发(二)使用自动脚本创建cocos2d-x 3.0项目