`
berdy
  • 浏览: 509031 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

扩展ImageView使可旋转

阅读更多
继承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>
0
0
分享到:
评论
2 楼 day5678 2013-12-27  
天秤丶 写道
麻烦问一下。。那个upon:angle不管用啊。。。


那你就在JAVA代码中设值呗。
1 楼 天秤丶 2013-11-08  
麻烦问一下。。那个upon:angle不管用啊。。。

相关推荐

    TextImageView,在图像上绘制可展开文本的imageview子类.zip

    一个ImageView子类,它在图像上绘制一个或多个可扩展的、可旋转的、可伸缩的文本。

    MaterialImageView-可设置阴影 、圆角的ImageView .zip

    项目地址:https://github.com/zhaozhentao/MaterialImageView 效果图:如何使用:由于MaterialImageView是扩展了ImageView,支持ImageView的所有特性。  android:src="@drawable/pic1"  android:layout_width=...

    Google Android SDK开发范例大全(第3版) 1/5

    每个范例后面均有扩展学习,在学习范例应用的同时延伸思考。汲取专家经验,指引入门捷径。 移动网络设备(MID,Mobile Internet Device)的发展趋势锐不可当,其中以智能手机最受瞩目。 《Google Android SDK开发...

    Google Android SDK开发范例大全(第3版) 4/5

    每个范例后面均有扩展学习,在学习范例应用的同时延伸思考。汲取专家经验,指引入门捷径。 移动网络设备(MID,Mobile Internet Device)的发展趋势锐不可当,其中以智能手机最受瞩目。 《Google Android SDK开发...

    Google Android SDK开发范例大全(第3版) 3/5

    每个范例后面均有扩展学习,在学习范例应用的同时延伸思考。汲取专家经验,指引入门捷径。 移动网络设备(MID,Mobile Internet Device)的发展趋势锐不可当,其中以智能手机最受瞩目。 《Google Android SDK开发...

    Google Android SDK开发范例大全(第3版) 5/5

    每个范例后面均有扩展学习,在学习范例应用的同时延伸思考。汲取专家经验,指引入门捷径。 移动网络设备(MID,Mobile Internet Device)的发展趋势锐不可当,其中以智能手机最受瞩目。 《Google Android SDK开发...

    精通ANDROID 3(中文版)1/2

    6.2.3 ImageView控件  6.2.4 日期和时间控件  6.2.5 MapView控件  6.3 适配器  6.3.1 SimpleCursorAdapter  6.3.2 了解ArrayAdapter  6.4 结合使用适配器和AdapterView  6.4.1 基本的列表控件:...

    精通Android 3 (中文版)2/2

    6.2.3 ImageView控件  6.2.4 日期和时间控件  6.2.5 MapView控件  6.3 适配器  6.3.1 SimpleCursorAdapter  6.3.2 了解ArrayAdapter  6.4 结合使用适配器和AdapterView  6.4.1 基本的列表控件:...

Global site tag (gtag.js) - Google Analytics