编写App的开场Activity 和 扩展ImageView使可旋转
2013-10-11 22:18
155 查看
http://berdy.iteye.com/blog/1768905
在android的app和游戏的应用中,都会有个开场场景,老外管这个叫splash。
现在就编写个简单的SplashActivity
Java代码
![](http://berdy.iteye.com/images/icon_star.png)
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代码
![](http://berdy.iteye.com/images/icon_star.png)
<?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代码
![](http://berdy.iteye.com/images/icon_star.png)
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代码
![](http://berdy.iteye.com/images/icon_star.png)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="RotateImageView">
<attr name="angle" format="integer" />
</declare-styleable>
</resources>
使用UponRotateImageView
Xml代码
![](http://berdy.iteye.com/images/icon_star.png)
<?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>
在android的app和游戏的应用中,都会有个开场场景,老外管这个叫splash。
现在就编写个简单的SplashActivity
Java代码
![](http://berdy.iteye.com/images/icon_star.png)
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代码
![](http://berdy.iteye.com/images/icon_star.png)
<?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代码
![](http://berdy.iteye.com/images/icon_star.png)
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代码
![](http://berdy.iteye.com/images/icon_star.png)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="RotateImageView">
<attr name="angle" format="integer" />
</declare-styleable>
</resources>
使用UponRotateImageView
Xml代码
![](http://berdy.iteye.com/images/icon_star.png)
<?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
- [转]Android UI:看看Google官方自定义带旋转动画的ImageView-----RotateImageView怎么写(附 图片淡入淡出效果)
- 安卓控件使用系列16:ImageView实现图片缩放和旋转
- Android中综合使用AsyncHttpClient和SmartImageView编写新闻客户端的案例
- 点击ImageView旋转固定角度
- Android图片旋转,缩放,位移,倾斜,对称完整示例(一)——imageView.setImageMatrix(matrix)和Matrix
- Swift 旋转动画 (设置的ImageView的旋转动画)
- Android UI:看看Google官方自定义带旋转动画的ImageView-----RotateImageView怎么写(附 图片淡入淡出效果)
- Bitmap与Matrix旋转ImageView
- GLSurfaceView中添加button组件(示例是多activity中,使用openGL画一彩色旋转立方体)
- ImageView实现图像的放大缩小和旋转功能
- 编写 Firefox 扩展,用于旋转在线地图应用程序中的图像
- 【IOS】扩展UIImageViewEx实现:手势移动,旋转,缩放(附带一个收缩的文字标签功能)
- Android图片旋转,缩放,位移,倾斜,对称完整示例(一)——imageView.setImageMatrix(matrix)和Matrix
- Ubuntu安装 扩展名为 appimage 的文件
- [一个星期自学安卓]使用ImageView显示图像的缩放与旋转
- Android图片旋转,缩放,位移,倾斜,对称完整演示样例(一)——imageView.setImageMatrix(matrix)和Matrix
- android自定义控件:可旋转View:可作为ImageView、ImageButton
- Android在打开一个新的Activity浏览大图时使用ImageView/PhotoView的时候图片显示在屏幕上部的问题
- 安卓仿手机网易新闻app项目开发系列之(二)轮播图显示和RecyclerView适配器编写