android xml设置矩形,在canvas中将drawRect方法绘制的5个矩形添加到android的layout.xml中...

我使用Canvas中的drawRect方法在屏幕上创建了5个不同颜色的矩形。我想在屏幕的底部添加一个搜索栏,当它从左向右移动时,它会逐渐改变矩形的颜色。我在将矩形添加到布局和程序崩溃时遇到问题。在canvas中将drawRect方法绘制的5个矩形添加到android的layout.xml中

这里是我的矩形类,吸引了5个矩形:

public class RectAngle extends View {

public RectAngle(Context context) {

super(context);

setFocusable(true);

}

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

float x = getWidth();

float y = getHeight();

Paint topLeftRect = new Paint();

Paint bottomLeftRect = new Paint();

Paint topRightRect = new Paint();

Paint midRightRect = new Paint();

Paint bottomRightRect = new Paint();

// Draw the top left rectangle

topLeftRect.setStyle(Paint.Style.FILL);

topLeftRect.setColor(Color.parseColor("#FFFF99"));

// canvas.drawPaint(topLeftRect);

canvas.drawRect(0, 0, x/2, y/2, topLeftRect);

//Draw the bottom left rectangle

bottomLeftRect.setStyle(Paint.Style.FILL);

//bottomLeftRect.setColor(Color.WHITE);

// canvas.drawPaint(bottomLeftRect);

bottomLeftRect.setColor(Color.parseColor("#FFFFFF"));

canvas.drawRect(0, y/2, x/2, y, bottomLeftRect);

//Draw the top tight rectangle

topRightRect.setStyle(Paint.Style.FILL);

//topRightRect.setColor(Color.WHITE);

topRightRect.setColor(Color.parseColor("#FF6600"));

//canvas.drawPaint(topRightRect);

canvas.drawRect(x/2, 0, x, y/3, topRightRect);

// Draw the middle right rectangle

midRightRect.setStyle(Paint.Style.FILL);

//midRightRect.setColor(Color.WHITE);

midRightRect.setColor(Color.parseColor("#66FFFF"));

// canvas.drawPaint(midRightRect);

canvas.drawRect(x/2, y/3, x, 2*y/3, midRightRect);

//Draw the bottom right rectangle

bottomRightRect.setStyle(Paint.Style.FILL);

//bottomRightRect.setColor(Color.WHITE);

bottomRightRect.setColor(Color.parseColor("#CCCC00"));

// canvas.drawPaint(bottomRightRect);

canvas.drawRect(x/2, 2*y/3, x, y, bottomRightRect);

}

}

这是我的主要活动类:

public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

//setContentView(new RectAngle(this));

setContentView(R.layout.activity_main);

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.menu_main, menu);

return true;

}

@Override

public boolean onOptionsItemSelected(MenuItem item) {

// Handle action bar item clicks here. The action bar will

// automatically handle clicks on the Home/Up button, so long

// as you specify a parent activity in AndroidManifest.xml.

int id = item.getItemId();

//noinspection SimplifiableIfStatement

if (id == R.id.action_settings) {

return true;

}

return super.onOptionsItemSelected(item);

}

}

我知道的setContentView(新的Rectangle(本))调用绘制矩形在屏幕上,但它不会添加它将被查找栏使用的XML文件。

这是我的activity_main.xml中的文件:

xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"

android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

android:id="@+id/rectangle"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/seekBar"

android:layout_alignParentBottom="true"

android:layout_centerHorizontal="true"

android:max="100" />

感谢您的帮助。

2015-10-16

HassanB

+0

发布错误日志后! –

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值