1

我有两个ImageViews位于另一个之上,并且都位于frameLayout. 据我所知,我们添加的任何内容都将位于frameLayout屏幕的左上方,我们可以通过为它们设置 a 来设置它们的位置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

4

1 回答 1

0

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

透视视图.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>
于 2013-10-20T03:27:10.993 回答