Android设置Pivot方法有效吗?

时间:2013-10-19 22:56:54

标签: android pivot rotateanimation

我有两个ImageViews其中一个位于另一个之上且都在frameLayout内。据我所知,我们在frameLayout添加的内容将位于屏幕的左上方,我们可以通过设置padding来设置其位置,因此我设置了paddings两个ImageViews

upperImage = (ImageView) findViewById(R.id.imageView1);
        upperImage.setPadding(250,250, 250, 0);

lowerImage = (ImageView) findViewById(R.id.imageView2);
        lowerImage.setPadding(250, 361, 250, 0);    //250+Height of Image

我想转动我的ImageViews,我差点就完成了。我还没有做的唯一事情就是枢轴的位置。

我想在ImageViews的中间底部设置轴心点。我尝试了很多数字,但我没有得到答案,甚至没有发现该方法是如何工作的。我的意思是我不知道它作为一个论点得到的数字是多少。是pixels还是什么?(我在onCreate方法中调用动画方法,因此我无法使用getWidth()getHeight()方法,因此我输入这些方法的数字回归自己。)

我甚至尝试过不同的地方在java甚至XML layout设置支点。但这没有任何改变。

我想知道的是如何使用ImageView方法将枢轴点设置在setPivot的中心底部?(它们似乎不是工作方法)可能是问题吗?在onCreate方法中设置点?如果是,我如何才能在onCreate中使其工作?

1 个答案:

答案 0 :(得分:0)

请记住将实际图形放在位图的res文件夹中,并在构造函数中更改名称或加载位图时的任何名称

PivotView.java

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class PivotView extends View{
    public Bitmap arm;
    public Bitmap hand;
    public int armrotation = 0;
    public int handrotation = 0;
public PivotView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        arm = BitmapFactory.decodeResource(getResources(),R.drawable.arm );
        hand = BitmapFactory.decodeResource(getResources(),R.drawable.hand );
    }

    @Override
    protected void onDraw (Canvas canvas){
        canvas.rotate((float)armrotation, 0, (float)canvas.getHeight());
        canvas.drawBitmap(arm, new Rect(0,0,arm.getWidth(),arm.getHeight()), new Rect(0,
                canvas.getHeight()*1/3,canvas.getWidth()*1/5,canvas.getHeight()), null);
        Matrix matrix = new Matrix();

        matrix.setRectToRect(new RectF(0,0,(float)hand.getWidth(),(float)hand.getHeight()), new RectF(0,
                (float)canvas.getHeight()*1/4,(float)canvas.getWidth()*1/5,(float)canvas.getHeight()*4/10), Matrix.ScaleToFit.FILL);

        matrix.preRotate((float)handrotation,(float)hand.getWidth()/2,(float)hand.getHeight());
        canvas.drawBitmap(hand, matrix, null);


    }
    public void rotatArm(int rotate){
        Log.d("arm", String.valueOf(rotate));
        armrotation = rotate;

    }
    public void rotateHand(int rotate){
        Log.d("hand", String.valueOf(rotate));
        handrotation = rotate;

    }
}

MainActivity.java

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class MainActivity extends Activity implements SeekBar.OnSeekBarChangeListener{

    public SeekBar hand;
    public SeekBar arm;
    public PivotView robot;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        hand = (SeekBar)findViewById(R.id.seekhand);
        hand.setMax(360);
        arm = (SeekBar)findViewById(R.id.seekarm);
        arm.setMax(90);
        robot = (PivotView)findViewById(R.id.pivot);
        hand.setOnSeekBarChangeListener(this);
        arm.setOnSeekBarChangeListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromUser) {
        // TODO Auto-generated method stub
        switch(seekBar.getId()){
        case R.id.seekarm:
            rotatethearm(progress);
            //robot.invalidate();
            break;
        case R.id.seekhand:
            rotatethehand(progress);
            //robot.invalidate();
            break;
        }
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub

    }
    public void rotatethearm(int progress){
        robot.rotatArm(progress);
        robot.invalidate();

    }
    public void rotatethehand(int progress){
        robot.rotateHand(progress);
        robot.invalidate();
    }

}

使用自定义视图制作您的布局,请记住使用您的包名称,在自定义视图的布局中显示com.example.pivotview

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
<com.example.pivotview.PivotView
    android:id="@+id/pivot"
    android:layout_width="match_parent"
    android:layout_height="match_parent"></com.example.pivotview.PivotView>

   <SeekBar 
       android:id="@+id/seekarm"
       android:layout_alignParentBottom = "true"
       android:layout_width = "match_parent"
       android:layout_height="wrap_content"/>

   <SeekBar
       android:id="@+id/seekhand"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_above="@+id/seekarm"
        />

</RelativeLayout>