简体   繁体   English

android按钮旋转与设备方向

[英]android button rotation with device orientation

in my android app, when i rotate/tilt my device, i want to rotate my button controls in the opposite direction, which compensates the device's orientation. 在我的android应用中,当我旋转/倾斜设备时,我想以相反的方向旋转按钮控件,以补偿设备的方向。 kinda like the idea of screen orientation in android 有点像android中屏幕方向的想法

here is my sensor class: 这是我的传感器类:

@Override
public void onSensorChanged(SensorEvent event) {
    // TODO Auto-generated method stub
    synchronized (this) {
        double alpha = 0.8;
        double gravity[] = new double[3];
        switch (event.sensor.getType()){
            case Sensor.TYPE_ACCELEROMETER:
                // alpha is calculated as t / (t + dT)
                // with t, the low-pass filter's time-constant
                // and dT, the event delivery rate                                  
                //gravity[0] = alpha * gravity[0] + (1 - alpha) * event.values[0];
                //gravity[1] = alpha * gravity[1] + (1 - alpha) * event.values[1];
                //gravity[2] = alpha * gravity[2] + (1 - alpha) * event.values[2];

                //sensorX = event.values[0] - gravity[0];
                //sensorY = event.values[1] - gravity[1];
                //sensorZ = event.values[2] - gravity[2];
                    //ignore this section!
            break;
        case Sensor.TYPE_ORIENTATION:   
                sensorZ = event.values[0];
                sensorX = event.values[1];
                sensorY = event.values[2];
                outputZ.setText("Z:"+Float.toString(event.values[0]));
                outputX.setText("X:"+Float.toString(event.values[1]));
                outputY.setText("Y:"+Float.toString(event.values[2]));
                //Toast.makeText(getApplicationContext(), "SWITCH TO ",Toast.LENGTH_LONG).show();
                //RelativeLayout s_r_t = (RelativeLayout) findViewById(R.id.snap_r_t);
                //RotateView r_flash = new RotateView(this , s_r_t.getWidth(), s_r_t.getHeight(), 90)(RotateView) findViewById(R.id.flash);
                //RotateView r_flash = new RotateView(this , s_r_t.getWidth(), s_r_t.getHeight(), 90);
                s_r_t.addView(r_flash);
                //s_r_t.setLayoutParams(params)
                //flash.getWidth();
                //flash.getHeight();

        break;

        }
    }

}

along with my extended button: 以及我的扩展按钮:

 public class RotateView extends Button {

private float cx,cy;
private double degree;
private Button button;

public RotateView(Context context , int x, int y, double deg) {
    super(context);     
    cx = (float)x/2;
    cy = (float)y/2;
    degree = deg;
}

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

@Override
protected void onDraw(Canvas canvas) {
     canvas.save();
     canvas.rotate((float)degree,cx,cy);
     super.onDraw(canvas);
     canvas.restore();

}

}

how do i create my button with the extended button class? 如何使用扩展按钮类创建按钮?

here is my button: 这是我的按钮:

 flash = (Button)findViewById(R.id.flash);

basically, this is what i want to achieve(pseudo code): 基本上,这就是我想要实现的(伪代码):

new button = find my button id;
onSensorChanged{
 set button rotation to sensor.getdata degrees;
}

if possible, please give complete segments of the code, i've seen many answers almost answered my question, but missing the essential part of how to implement the extended button back to my button in my main class. 如果可能的话,请提供完整的代码段,我已经看到许多答案几乎可以回答我的问题,但是却缺少如何在我的主类中实现扩展按钮回到我的按钮的基本部分。

    ImageButton button= (ImageButton) findViewById(R.id.test_image);
    Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.test);
    Matrix mat = new Matrix();
    mat.postRotate(deg);
    Bitmap bMapRotate = Bitmap.createBitmap(bMap, 0, 0, bMap.getWidth(), bMap.getHeight(), mat, true);
    button.setImageBitmap(bMapRotate);

for future reference, follow the guide on android dev about custom components; 为了将来参考,请遵循android dev上有关自定义组件的指南;

in my case, i've forgot to add the xml layout 就我而言,我忘记添加xml布局

the procedure goes like this-> 程序是这样的->

  1. Create a class for the component then override parts that i want to change 为组件创建一个类,然后覆盖我要更改的部分

    in this case: 在这种情况下:

     void setValue(double deg) {//this changes the rotation of canvas.... degree = deg; } 
  2. add xml for the custom class 为自定义类添加xml

  3. find view in my activity and do changes 在我的活动中查找视图并进行更改

done 完成

您可以使用button.animate()。rotation(degree);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM