実際のグラフィックをビットマップの 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>