继承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>
分享到:
相关推荐
一个ImageView子类,它在图像上绘制一个或多个可扩展的、可旋转的、可伸缩的文本。
项目地址:https://github.com/zhaozhentao/MaterialImageView 效果图:如何使用:由于MaterialImageView是扩展了ImageView,支持ImageView的所有特性。 android:src="@drawable/pic1" android:layout_width=...
每个范例后面均有扩展学习,在学习范例应用的同时延伸思考。汲取专家经验,指引入门捷径。 移动网络设备(MID,Mobile Internet Device)的发展趋势锐不可当,其中以智能手机最受瞩目。 《Google Android SDK开发...
每个范例后面均有扩展学习,在学习范例应用的同时延伸思考。汲取专家经验,指引入门捷径。 移动网络设备(MID,Mobile Internet Device)的发展趋势锐不可当,其中以智能手机最受瞩目。 《Google Android SDK开发...
每个范例后面均有扩展学习,在学习范例应用的同时延伸思考。汲取专家经验,指引入门捷径。 移动网络设备(MID,Mobile Internet Device)的发展趋势锐不可当,其中以智能手机最受瞩目。 《Google Android SDK开发...
每个范例后面均有扩展学习,在学习范例应用的同时延伸思考。汲取专家经验,指引入门捷径。 移动网络设备(MID,Mobile Internet Device)的发展趋势锐不可当,其中以智能手机最受瞩目。 《Google Android SDK开发...
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 基本的列表控件:...
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 基本的列表控件:...