开发者

Merging two views in android programming

i have 2 activities first activity continously changes the screencolor using renderview class, 2nd activity displays the mouse events using the textview which is set to contextview. How can i display the mouseevents while my background keep on changing colors. I am new to the android programming.please let me know if my question is not clear..

Code for changing background:

     public class renderviewtest extends Activity {
public class renderviewtest extends 开发者_高级运维Activity {
class RenderView extends View {
Random rand = new Random();
public RenderView(Context context) {
super(context);
}
protected void onDraw(Canvas canvas) {
canvas.drawRGB(rand.nextInt(256),rand.nextInt(256),rand.nextInt(256));
invalidate();
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new RenderView(this));
}
}

Code for mouse events:
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;

public class singletouchtest extends Activity implements OnTouchListener {
StringBuilder builder = new StringBuilder();
TextView textView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
textView = new TextView(this);
textView.setText("Touch and drag (one finger only)!");
textView.setOnTouchListener(this);
setContentView(textView);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
builder.setLength(0);
switch (event.getAction()) {   
case MotionEvent.ACTION_DOWN:
builder.append("down, ");
break;
case MotionEvent.ACTION_MOVE:
builder.append("move, ");
break;
case MotionEvent.ACTION_CANCEL:
builder.append("cancle, ");
break;
case MotionEvent.ACTION_UP:
builder.append("up, ");
break;
}
builder.append(event.getX());
builder.append(", ");
builder.append(event.getY());
String text = builder.toString();
textView.setText(text);
return true;
}
}    
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜