Im trying to build a piano app, i have gone through this link
i was able to create view which draws series of black and white keys.I dont know how to scroll horizontally, Since iam not using any xml file for the view.
Here is the code for My MainActivity:
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyTouch(this));
//setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Here is the code for MyTouch: the same code specified in the above link.
public class MyTouch extends View {
public MyTouch(Context context) {
super(context);
}
Bitmap whiteKey, blackKey;
Paint paint = new Paint();
public void draw(Canvas canvas) {
if (whiteKey == null) {
whiteKey = BitmapFactory.decodeResource(getResources(), R.drawable.white);
}
if (blackKey == null) {
blackKey = BitmapFactory.decodeResource(getResources(), R.drawable.black);
}
int keys = 5;
// draw white keys
for (int i = 0; i < keys; i++) {
canvas.drawBitmap(whiteKey, i * whiteKey.getWidth(), 0, paint);
}
// draw black keys
for (int i = 0; i < keys; i++) {
if (i != 3 && i != 7) {
canvas.drawBitmap(blackKey, i * blackKey.getWidth()+blackKey.getWidth()*0.5f, 0, paint);
}
}
}
};
I have searched Google,and tried many examples, but none of them is working for me.More over this view contains a drawn canvas, and iam new to this canvas and drawings !!