1

I have two ImageViews one of them located on top of another and both inside a frameLayout. As far as I know whatever we add in frameLayout will sit on the left top part of screen and we can set their position by setting a padding for them, So I've set paddings for both of 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

I want to rotate my ImageViews and I'm almost done with that. the only thing I still haven't done is the location of pivot.

I want to set the pivot point on the center bottom of my ImageViews. I've tried many numbers but I didn't get the answer and didn't even find out how that method works. I mean I don't know what's the number it gets as an argument. Is that pixels or what?(I call the method for animations in onCreate method so I can't use getWidth() and getHeight() methods so I enter the numbers that these methods return myself.)

I have even tried different places to set pivot point in java and even XML layout. But that didn't make any changes.

All I want to know is how to set the pivot point at center bottom of my ImageView using setPivot methods?(They don't seem to be working methods) Can it be the problem with setting the point in onCreate method? If yes how can I make it work in onCreate?

  • you been stuck on this for a bit, nobody posted on your question so instead of it being lost in the abyss of questions i made it with the custom view how i was telling you before, you can tweak it up to whatever specifications youd like :) – JRowan Oct 20 '13 at 03:39

1 Answers1

0

Remember to put the actual graphic in the res folders for your bitmap and change the name in the constructor or whatever when it loads the bitmap

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();
    }

}

Make your layout like so with custom view, remember to use your package name in place where it says com.example.pivotview in the layout for the custom view

<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>
JRowan
  • 6,824
  • 8
  • 40
  • 59