Android 画布的旋转
2010-10-12 02:12:00 来源:本站整理有时候我们会遇到这样的情况:一些字需要从下到上竖着显示怎么办?你会想textview是否有这样的属性设置呢?很不幸的是,并不存在这样方便的属性,你必须自己定义一个view才能符合这一的要求。那如何实现呢?在我们从在重载一个view的时候,都会在onDraw的时候做自己想做的事情,那么我们在这个函数里面将画布进行旋转-90度就可以得到这样的效果,因为此时屏幕的坐标(0,0)点由左上角变成左下角,而系统画东西都是根据原点坐标来画的,这样选择以后的坐标系统(x1,y1)与原来的坐标系统(x0,y0)的对应关系是(x1=-y0,y1=x0),若旋转90度,那就是(x1=-y0,y1=-x0).
Java代码
/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.apis.view;
// Need the following import to get access to the app resources, since this
// class is in a sub-package.
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import com.example.android.apis.R;
/**
* Example of how to write a custom subclass of View. LabelView is used to draw
* simple text views. Note that it does not handle styled text or
更多精彩
赞助商链接