android开发问题: 在Activity主界面绘制画布,可是它把整个窗口都覆盖掉了,怎么设定画布的窗口大小呢(
代码如下:packageframe.src;importandroid.app.Activity;importandroid.content.Context;import...
代码如下:
package frame.src;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.ShapeDrawable;
import android.os.Bundle;
import android.view.View;
public class FrameAmationActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setContentView(new Drawing(this));
Drawing.resolveSize(10, 10);
}
class Drawing extends View implements Runnable{
Bitmap[] bitmap = new Bitmap[6];
//private ShapeDrawable mDrawable;
//创建一个画笔
Paint paint=new Paint();
//保存图片切换的变量
int temp=0;
//创建线程
Thread t=new Thread(this);
//构造方法
public Drawing(Context context) {
super(context);
bitmap[0] = BitmapFactory.decodeResource(this.getResources(),R.drawable.f1);
bitmap[1] = BitmapFactory.decodeResource(this.getResources(),R.drawable.f2);
bitmap[2] = BitmapFactory.decodeResource(this.getResources(),R.drawable.f3);
bitmap[3] = BitmapFactory.decodeResource(this.getResources(),R.drawable.f4);
bitmap[4] = BitmapFactory.decodeResource(this.getResources(),R.drawable.f5);
bitmap[5] = BitmapFactory.decodeResource(this.getResources(),R.drawable.f6);
//开始线程
t.start();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//设置画布颜色
//canvas.setViewport();
canvas.drawColor(Color.GRAY);
canvas.drawLine(0, 0, 100, 100, paint);
canvas.drawCircle(30, 100, 30, paint);
canvas.translate(20, 10);
//canvas.setSize(100 ,100);
//canvas.scale(30, 30);
//canvas.clipRect(10, 10, 10, 20);
//canvas.draw
//绘制图片
canvas.drawBitmap(bitmap[temp], 100, 20, paint);
}
public void run() {
while(true){
temp++;
if(temp==5){
temp=0;
}
//刷新屏幕,间隔70毫秒
this.postInvalidate();
try {
Thread.sleep(70);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
我实现的是动画 展开
package frame.src;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.ShapeDrawable;
import android.os.Bundle;
import android.view.View;
public class FrameAmationActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setContentView(new Drawing(this));
Drawing.resolveSize(10, 10);
}
class Drawing extends View implements Runnable{
Bitmap[] bitmap = new Bitmap[6];
//private ShapeDrawable mDrawable;
//创建一个画笔
Paint paint=new Paint();
//保存图片切换的变量
int temp=0;
//创建线程
Thread t=new Thread(this);
//构造方法
public Drawing(Context context) {
super(context);
bitmap[0] = BitmapFactory.decodeResource(this.getResources(),R.drawable.f1);
bitmap[1] = BitmapFactory.decodeResource(this.getResources(),R.drawable.f2);
bitmap[2] = BitmapFactory.decodeResource(this.getResources(),R.drawable.f3);
bitmap[3] = BitmapFactory.decodeResource(this.getResources(),R.drawable.f4);
bitmap[4] = BitmapFactory.decodeResource(this.getResources(),R.drawable.f5);
bitmap[5] = BitmapFactory.decodeResource(this.getResources(),R.drawable.f6);
//开始线程
t.start();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//设置画布颜色
//canvas.setViewport();
canvas.drawColor(Color.GRAY);
canvas.drawLine(0, 0, 100, 100, paint);
canvas.drawCircle(30, 100, 30, paint);
canvas.translate(20, 10);
//canvas.setSize(100 ,100);
//canvas.scale(30, 30);
//canvas.clipRect(10, 10, 10, 20);
//canvas.draw
//绘制图片
canvas.drawBitmap(bitmap[temp], 100, 20, paint);
}
public void run() {
while(true){
temp++;
if(temp==5){
temp=0;
}
//刷新屏幕,间隔70毫秒
this.postInvalidate();
try {
Thread.sleep(70);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
我实现的是动画 展开
2个回答
展开全部
你是说View占满了整个屏幕吗?这样的话就只能在layout中写一个布局文件,将View以一种组件的方式声明到.xml文件中,然后你可以在它的上下中声明其他的button或者textview或者ImageButton等。
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
canvas.drawCircle(30, 100, 30, paint);
canvas.drawBitmap(bitmap[temp], 100, 20, paint);
仔细看看画图的起始坐标,原始坐标(0,0)为屏幕的左上角,向右为X的正方向,向下为Y轴的正方向。把握好坐标就好了。
canvas.drawBitmap(bitmap[temp], 100, 20, paint);
仔细看看画图的起始坐标,原始坐标(0,0)为屏幕的左上角,向右为X的正方向,向下为Y轴的正方向。把握好坐标就好了。
追问
不行啊,那只会将整个画布向右下角移动,我是想让整个画布缩小,比如说缩小后放在中间,网上有说用canvas.setViewport(width, height);这个的,可我不会,还望大侠请教!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询