gpt4 book ai didi

android - 有没有办法在android 2.1中不使用动画来旋转按钮

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:19:01 25 4
gpt4 key购买 nike

我正在寻找旋转按钮的方法。不使用动画..!

我不想使用动画因为 this.

如果有人有任何想法,请帮助我。

xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="@+id/ll" android:gravity="center_vertical">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/hello" />
<com.mind.RotateButton android:layout_width="100dp" android:gravity="center_vertical"
android:layout_height="100dp" android:id="@+id/ll1">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Button" android:id="@+id/but" />
</com.mind.RotateButton>
</LinearLayout>

代码

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.but);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Hello", Toast.LENGTH_SHORT).show();
}
});
}

提前致谢......!

最佳答案

扩展按钮类:

public class RotateButton extends Button{

public RotateButton(Context context) {
super(context);
}

public RotateButton(Context context, AttributeSet attrs) {
super(context, attrs);
}

@Override
protected void onDraw(Canvas canvas) {
canvas.save();
canvas.rotate(45, getWidth() / 2, getHeight() / 2);
super.onDraw(canvas);
canvas.restore();
}

}

在你的布局中:

<com.samples.myapp.ui.RotateButton
android:layout_height="wrap_content" android:id="@+id/MyBtn"
android:padding="5dip" android:textColor="@color/darkGreen"
android:textSize="16dip" android:text="TextView"
android:layout_width="wrap_content"></com.samples.myapp.ui.RotateButton>

-------------------------------------------- --------------------------

编辑:

另一种方法:设计一个可旋转的 LinearLayout 并将您的控件放入其中。 LinearLayout 可以完全旋转:

package org.mabna.order.ui;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.view.MotionEvent;

public class RotateLinearLayout extends LinearLayout{

private Matrix mForward = new Matrix();
private Matrix mReverse = new Matrix();
private float[] mTemp = new float[2];

public RotateLinearLayout(Context context) {
super(context);
}

public RotateLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}

@Override
protected void dispatchDraw(Canvas canvas) {

canvas.rotate(180, getWidth() / 2, getHeight() / 2);

mForward = canvas.getMatrix();
mForward.invert(mReverse);
canvas.save();
canvas.setMatrix(mForward); // This is the matrix we need to use for
// proper positioning of touch events
super.dispatchDraw(canvas);
canvas.restore();
invalidate();
}

@Override
public boolean dispatchTouchEvent(MotionEvent event) {
event.setLocation(getWidth() - event.getX(), getHeight() - event.getY());
return super.dispatchTouchEvent(event);
}
}

关于android - 有没有办法在android 2.1中不使用动画来旋转按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8047422/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com