I'm tryin to draw a shape on a canvas under all buttons.
Here's the code:
Paint paint = new Paint();
paint.setColor(R.color.Kolor);
View view;
LayoutInflater inflater = (LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.menu_glowne, null);
Canvas can = new Canvas(Bitmap.createBitmap(300, 300, Bitmap.Config.ARGB_8888));
can.drawRect(0, 0, 200, 200, paint);
setContentView(view);
view.draw(can);
Don't know why I'm still getting the layout without anything drawn underneath.
Any ideas on what I'm doing wrong?
Thanx in advance!
In your example you are drawing the view on your canvas rather than on canvas of the view.
You should use a simple approach, don't inflate your layout but load it normally, then find the root container and set its background. Something like this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Paint paint = new Paint();
paint.setColor(Color.MAGENTA);
Bitmap bgr = Bitmap.createBitmap(480, 800, Bitmap.Config.ARGB_8888);
Canvas can = new Canvas(bgr);
can.drawRect(50, 50, 200, 200, paint);
LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
ll.setBackgroundDrawable(new BitmapDrawable(bgr));
}
main layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/ll">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/>
</LinearLayout>
You don't draw this way in android. Everything happens through a method override (onDraw). Follow this tutorial to get started :
http://www.helloandroid.com/tutorials/how-use-canvas-your-android-apps-part-1
Regards,
Stéphane
Related
I want to show a statistic page as a popup from an activity. My question is how do I hand off the data to the canvas drawing it?
My popupWindow xml is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/gradient_dark_teal_bg">
<LinearLayout
android:id="#+id/llvStats"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/gradient_teal_bg"
android:orientation="vertical">
<TextView
android:id="#+id/txtBanner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:gravity="center_vertical|center_horizontal|center"
android:text="STATISTICS"
android:textColor="#color/colorTeal1"
android:textSize="24sp"
android:textStyle="bold|italic" />
<com.mairyu.app.lingoflash.StatsDrawCanvas
android:id="#+id/statsCanvasDraw"
android:layout_below="#+id/txtBanner"
android:layout_width="400dp"
android:layout_height="500dp"
android:layout_column="1"
android:layout_gravity="left|top"
android:background="#drawable/gradient_light_teal_bg"
android:layout_row="0" />
</LinearLayout>
</RelativeLayout>
My popup launch snippet is:
case R.id.btnDatabaseStats:
layoutInflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
popupView = layoutInflater.inflate(R.layout.stats_popup_window,null);
popupWindow = new PopupWindow(popupView,STATS_POPUP_WIDTH,STATS_POPUP_HEIGHT);
popupWindow.showAtLocation(findViewById(R.id.database_page),
Gravity.CENTER, STATS_POPUP_GRAVITY_X, STATS_POPUP_GRAVITY_Y);
LinearLayout llvStats = (LinearLayout) popupView.findViewById(R.id.llvStats);
llvStats.setOnTouchListener(new OnSwipeTouchListener(this) {
#Override
public void onSwipeLeft() {
popupWindow.dismiss();
}
});
I want to see something like this:
Since this is not a new activity, I can't use the intent/bundle method to exchange variables (or can I ?). If it would be simple textview or other widgets, I could use setText etc., but I need to get the data inside the canvas.
I think I still lack understanding of how to use onDraw and other canvas features, but right now I just hardcoded the numbers. Is there an easy way to simply get those numbers (5/15/25) from the launching activity ? (I tried a global variable, but it wasn't visible within onDraw)
#Override
protected void onDraw(Canvas c) {
CanvasWidth = c.getWidth();
CanvasHeight = c.getHeight();
Paint myPaint = new Paint();
myPaint.setStyle(Paint.Style.FILL);
myPaint.setColor(getResources().getColor(R.color.colorTeal1));
myPaint.setStrokeWidth(10);
c.drawRect(160, 60, 800, 110, myPaint);
c.drawRect(160, 160, 250, 210, myPaint);
c.drawRect(160, 260, 400, 310, myPaint);
brush.reset();
brush.setColor(getResources().getColor(R.color.colorTeal1));
brush.setTextSize(36);
brush.setTypeface(Typeface.DEFAULT_BOLD);
c.drawText("1 Day:", 20, 100, brush);
c.drawText("2 Days:", 20, 200, brush);
c.drawText("3 Days:", 20, 300, brush);
brush.setColor(getResources().getColor(R.color.colorTeal3));
c.drawText("5", 167, 99, brush);
c.drawText("15", 167, 198, brush);
c.drawText("25", 167, 298, brush);
}
Hope this is clear and not too amateurish ...
Actually seems to be pretty simple, now I'm just using a method call within my canvas class:
public void setBar(ArrayList<Integer> left, ArrayList<Integer> top, ArrayList<Integer> right, ArrayList<Integer> bottom) {
barCoordLeft = left;
barCoordTop = top;
barCoordRight = right;
barCoordBottom = bottom;
invalidate();
}
and access these global variables within onDraw.
In my activity where I launch the popup, I get the canvas via:
statsDrawCanvas = (StatsDrawCanvas) popupView.findViewById(R.id.statsCanvasDraw);
and then pass on the data via the method call:
statsDrawCanvas.setBar(CanvasBarLeftArray, CanvasBarTopArray, CanvasBarRightArray, CanvasBarBottomArray);
I'm doing some experiments and I'm trying to do something like this:
public class MyActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyImageView view = new MyImageView(this);
setContentView(view);
}
}
public class MyImageView extends ImageView {
public MyImageView(Context context) {
super(context);
View view = View.inflate(context, R.layout.my_view, null);
((TextView) view.findViewById(R.id.view_label)).setText(...);
Bitmap bitmap = Bitmap.createBitmap(50, 50, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
view.draw(c);
setImageBitmap(bitmap);
}
}
My R.layout.my_view layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#drawable/blue_spot"
android:gravity="center">
<TextView
tools:text="99"
android:id="#+id/view_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:textStyle="bold"/>
</LinearLayout>
My shape:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<corners android:radius="10dp"/>
<solid android:color="#5AB7FF"/>
</shape>
And I get a blank empty screen... any idea why?
I'm doing all this workarounds because later I want to do use myImageView.setImageMatrix().
Thanks.
Ok I got the problem. It's drawing nothing because that view hasn't been rendered. What I need is a way to render this view without drawing it (in canvas) and then draw that view to the bitmap. That can be achieved using:
view.setDrawingCacheEnabled(true);
view.measure(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.buildDrawingCache(true);
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
Now I just have to do some adjustments but I've got it drawn.
Thanks.
Just like the screenshot of Google Plus above, I want to show a circular avatar image in the action bar. What should I do?
You can do this programatically using a Bitmap:
First do this on your activity's onCreate():
getSupportActionBar().setDisplayShowHomeEnabled(true);
Drawable drawable = new BitmapDrawable(getResources(), createCircleBitmap(sourceBitmap));
getSupportActionBar().setIcon(drawable);
Here is the createCircleBitmap() method:
public Bitmap createCircleBitmap(Bitmap bitmapimg){
Bitmap output = Bitmap.createBitmap(bitmapimg.getWidth(),
bitmapimg.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmapimg.getWidth(),
bitmapimg.getHeight());
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawCircle(bitmapimg.getWidth() / 2,
bitmapimg.getHeight() / 2, bitmapimg.getWidth() / 2, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmapimg, rect, rect, paint);
return output;
}
PS: If you don't have the Bitmap, you can use this to transform a Drawable into a Bitmap:
Bitmap sourceBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.your_drawable);
Create circular imageview.
Create custom view for actionbar using the circular imageview
Inflate the view and display custom view as action bar.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#drawable/black_pattern" >
<TextView
android:id="#+id/title_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textAllCaps="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#fff"
android:textStyle="bold" />
<com.example.CircularImageView
android:id="#+id/circularimageView1"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="8dp"/>
</RelativeLayout>
Activity.java
ActionBar mActionBar = getActionBar();
mActionBar.setDisplayShowHomeEnabled(false);
mActionBar.setDisplayShowTitleEnabled(false);
LayoutInflater mInflater = LayoutInflater.from(this);
View mCustomView = mInflater.inflate(R.layout.actionbar, null);
mActionBar.setCustomView(mCustomView);
mActionBar.setDisplayShowCustomEnabled(true);
when you Start Creating a Project (You Need to Follow the Third Step Below)
Step 1: New Android Project
Step 2 : Configure Your Project
Step 3 : Configure Launcher Icon
You can Choose a shape as Circle
I hope u needed this answer
I have a android.graphics.Path variable which I want to convert into an ImageView in my activity.
What I do right now and does NOT work (but it does run without error, it just displays a blank activity) is:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Path p = ... // some bezier curves
PathShape pathShape = new PathShape(path, (float)20.0, (float)20.0);
ShapeDrawable shapeDrawable = new ShapeDrawable(pathShape);
ImageView testImage = (ImageView) findViewById(R.id.testImage);
testImage.setImageDrawable(shapeDrawable);
}
}
The corresponding activity_main.xml is:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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="com.tg.MindGames.set.MainActivity">
<ImageView
android:id="#+id/testImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
What did I do wrong and what is considered best practice to put Path into an ImageView?
Additional Information: I want to use the Paths to draw the images of cards in a simple card game. Several cards and additional text fields should be displayed on the activity at the same time, cards should be able to have an onClickListener.
The size of 20.0 x 20.0 is arbitrary and should be altered later when I use the ImageViews in a GridView.
so here you have a red triangle drawn on the view's background:
View v = new View(this);
Path path = new Path();
path.moveTo(0, 1);
path.lineTo(0.5f, 0);
path.lineTo(1, 1);
ShapeDrawable sd = new ShapeDrawable(new PathShape(path, 1, 1));
sd.getPaint().setColor(Color.RED);
v.setBackgroundDrawable(sd);
setContentView(v);
I have this Android program in which I want to display a Rectangle within the ImageView of the parent layout. The components are set on this hierarchy and following the layout listed below. The code to display the rectangle are also indicated below:
LinearLayout [Vertical]
Spinner
ImageView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Spinner
android:id="#+id/spinMap"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:listitem="#android:layout/simple_spinner_item" />
<ImageView
android:id="#+id/imageMap"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/black" />
</LinearLayout>
CODE:
public class MainActivty extends Activity {
private Display display;
private Spinner spinMap;
private ImageView imageMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initailize();
}
private void initailize() {
display = getWindowManager().getDefaultDisplay();
spinMap = (Spinner) findViewById(R.id.spinMap);
imageMap = (ImageView) findViewById(R.id.imageMap);
imageMap.draw(drawMap());
}
private Canvas drawMap() {
Canvas canvas = new Canvas(Bitmap.createBitmap(display.getWidth(), display.getHeight(), Bitmap.Config.RGB_565));
Paint paint = new Paint();
paint.setColor(Color.RED);
canvas.drawRect(10.0f, 10.0f, 100.0f, 100.0f, paint);
return canvas;
}
}
The problem is that when I run the application, the Rectangle don't get display. How can I display the Rectangle?
I think this can help you Android - Canvas drawLine inside ImageView
You should create bitmap with a rectangle and then feed it to ImageView