nil*_*ash 5 android ondraw android-linearlayout
我正在开发小型Android应用程序,其中我使用我的自定义线性布局类.在那个课程中,我试图绘制一个小三角形并尝试将其包含在我的线性布局中,但我无法做到这一点.我试着以下方式...
@SuppressLint("DrawAllocation")
public class SimpleLin extends LinearLayout {
public String TAG = "CustomviewActivity";
LinearLayout parentLayout;
public SimpleLin(Context context)
{
super(context);
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(inflater != null){
view = inflater.inflate(R.layout.main2, this);d.lin_llt);
parentLayout.setBackgroundResource(R.drawable.bcd);
}
}
public SimpleLin(Context context, AttributeSet attrs) {
super( context, attrs );
context1= context;
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Log.i("############################", "inside ondraw");
Paint p = new Paint();
p.setStyle(Style.FILL);
p.setColor(Color.RED);
Point point = new Point();
point.x = 80;
point.y = 80;
Path path = getEquilateralTriangle(point, 70, Direction.SOUTH);
Bitmap b = Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888);
canvas.drawPath(path, p);
}
public static Path getEquilateralTriangle(Point p1, int width, Direction direction) {
Point p2 = null, p3 = null;
if (direction == Direction.NORTH) {
p2 = new Point(p1.x + width, p1.y);
p3 = new Point(p1.x + (width / 2), p1.y - width);
}
else if (direction == Direction.SOUTH) {
p2 = new Point(p1.x + width,p1.y);
p3 = new Point(p1.x + (width / 2), p1.y + width);
}
else if (direction == Direction.EAST) {
p2 = new Point(p1.x, p1.y + width);
p3 = new Point(p1.x - width, p1.y + (width / 2));
}
else if (direction == Direction.WEST) {
p2 = new Point(p1.x, p1.y + width);
p3 = new Point(p1.x + width, p1.y + (width / 2));
}
Path path = new Path();
path.moveTo(p1.x, p1.y);
path.lineTo(p2.x, p2.y);
path.lineTo(p3.x, p3.y);
return path;
}
public enum Direction
{
NORTH, SOUTH, EAST, WEST;
}
@SuppressWarnings("deprecation")
public void initialiseImages()
{
invalidate();
}
}
Run Code Online (Sandbox Code Playgroud)
我在initialiseImages我的活动中调用方法,我想使用这个自定义布局.所以问题是当我使用invalidate()时它不会调用我的on draw方法.这就是为什么它没有绘制我的三角形.而且我也混淆了如何将三角形包含在我的parentlayout..我的代码中有错误..如何在android中绘制这样的形状...需要帮助...谢谢...
Abh*_*ndi 11
自定义线性布局代码(我修改了代码,以便您更容易理解)
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.Point;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.LinearLayout;
/**
* @author Atrix1987
*
*/
public class CustomView extends LinearLayout {
/**
* @param context
*/
public CustomView(Context context) {
super(context);
commonConstructor(context);
}
/**
* @param context
* @param attrs
*/
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
commonConstructor(context);
}
/**
*
*/
Paint trianglePaint;
/**
*
*/
Path trianglePath;
/**
* @param context
*/
private void commonConstructor(Context context) {
trianglePaint = new Paint();
trianglePaint.setStyle(Style.FILL);
trianglePaint.setColor(Color.RED);
Point point = new Point();
point.x = 80;
point.y = 80;
trianglePath = getEquilateralTriangle(point, 70, Direction.SOUTH);
}
@Override
protected void onDraw(Canvas canvas) {
Log.i("Sample", "inside ondraw");
//avoid creating objects in onDraw
canvas.drawPath(trianglePath, trianglePaint);
}
private Path getEquilateralTriangle(Point p1, int width, Direction direction) {
Log.i("Sample", "inside getEqui");
Point p2 = null, p3 = null;
if (direction == Direction.NORTH) {
p2 = new Point(p1.x + width, p1.y);
p3 = new Point(p1.x + (width / 2), p1.y - width);
} else if (direction == Direction.SOUTH) {
p2 = new Point(p1.x + width, p1.y);
p3 = new Point(p1.x + (width / 2), p1.y + width);
} else if (direction == Direction.EAST) {
p2 = new Point(p1.x, p1.y + width);
p3 = new Point(p1.x - width, p1.y + (width / 2));
} else if (direction == Direction.WEST) {
p2 = new Point(p1.x, p1.y + width);
p3 = new Point(p1.x + width, p1.y + (width / 2));
}
Path path = new Path();
path.moveTo(p1.x, p1.y);
path.lineTo(p2.x, p2.y);
path.lineTo(p3.x, p3.y);
return path;
}
public enum Direction {
NORTH, SOUTH, EAST, WEST;
}
}
Run Code Online (Sandbox Code Playgroud)
活动的代码(为了简单起见,我做了这个,你也可以在xml和setContentView中指定它):
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
/**
* @author Atrix1987
*
*/
public class SampleActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CustomView cv = new CustomView(getApplicationContext());
cv.setBackgroundColor(Color.WHITE);
setContentView(cv, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
}
}
Run Code Online (Sandbox Code Playgroud)
请浏览开发者网站链接以获取自定义视图,以获取更多信息.
希望这可以帮助.
PFB的截图
| 归档时间: |
|
| 查看次数: |
11824 次 |
| 最近记录: |