我使用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
发布错误日志后! –