简体   繁体   English

Android:围绕中心旋转图片?

[英]Android: rotate image around the center?

I want an image to rotate around its center, but my method is not working, it's clearly rotated around a different pivot point. 我希望图像围绕其中心旋转,但是我的方法不起作用,显然是围绕不同的枢轴点旋转。 I'm new to Android development, so you may find some serious mistakes in my code. 我是Android开发的新手,因此您可能会在我的代码中发现一些严重的错误。 Also, if I rotate the screen, the image is rotated back to its initial rotation. 另外,如果我旋转屏幕,图像也会旋转回其初始旋转。 Here are my files: 这是我的文件:

fragment_main.xml fragment_main.xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ScrollView01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivityFragment"
        android:background="@drawable/texture">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Πάγωσε η κόλαση..."
            android:textColor="@color/label_color"
            android:textSize="20dp"
            android:id="@+id/label_0"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:paddingTop="25dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Μπρρρ!"
            android:textColor="@color/label_color"
            android:textSize="20dp"
            android:id="@+id/label_1"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="50dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Κρύο"
            android:textColor="@color/label_color"
            android:textSize="20dp"
            android:id="@+id/label_2"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="75dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Έτσι κ'έτσι..."
            android:textColor="@color/label_color"
            android:textSize="20dp"
            android:id="@+id/label_3"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="100dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Καλό"
            android:textColor="@color/label_color"
            android:textSize="20dp"
            android:id="@+id/label_4"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="125dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Πολύ καλό!"
            android:textColor="@color/label_color"
            android:textSize="20dp"
            android:id="@+id/label_5"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="150dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Κάνεις κοιλιακούς!"
            android:textColor="@color/label_color"
            android:textSize="20dp"
            android:id="@+id/label_6"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="175dp" />

        <ImageView
            android:src="@drawable/thermostat"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/thermostat"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            android:paddingTop="@dimen/margin" />

        <ImageView
            android:src="@drawable/thermostat_slider"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/thermostat_slider"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            android:paddingTop="@dimen/margin" />

        <ImageView
            android:src="@drawable/inside_thing"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/inside_thing"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            android:paddingTop="@dimen/margin" />

        <ImageView
            android:src="@drawable/inside_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/inside_text"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            android:paddingTop="@dimen/margin" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Τι κάνω?"
            android:textColor="@color/label_color"
            android:textStyle="bold"
            android:id="@+id/title"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="@dimen/title_margin"/>

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/text"
            android:text="@string/help_text"
            android:textColor="@color/label_color"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="750dp" />

        </RelativeLayout>
</ScrollView>

MainActivityFragment.java MainActivityFragment.java

package antoni4040.barufakis;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.TextView;


/**
 * A placeholder fragment containing a simple view.
 */
public class MainActivityFragment extends Fragment {

    public MainActivityFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        final ImageView thermostat = (ImageView) rootView.findViewById(R.id.thermostat);
        final ImageView slider = (ImageView) rootView.findViewById(R.id.thermostat_slider);
        final TextView label_0 = (TextView) rootView.findViewById(R.id.label_0);
        label_0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RotateAnimation animation = new RotateAnimation(0, 90,
                        (slider.getLeft() + slider.getRight())/2,
                        (slider.getTop() + slider.getBottom())/2);
                animation.setDuration(1000);
                animation.setFillAfter(true);
                slider.startAnimation(animation);
                label_0.setTextColor(Color.parseColor("#00BFFF"));
            }
        });

        return rootView;
    }
}

Try This : 尝试这个 :

 Animation a = new RotateAnimation(0.0f, 90.0f,
                    Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                    0.5f);

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

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