user648037
user648037

Reputation:

Developing a custom speech bubble

I am new to Android application development. I am building an Android app where the user needs to be able to add speech bubbles (think comics) over existing images. I have some questions on how to implement this,

Please i need your help!!

Upvotes: 2

Views: 4547

Answers (3)

Saeed-rz
Saeed-rz

Reputation: 1443

Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setAntiAlias(true);
paint.setStrokeWidth(2);
paint.setStyle(Paint.Style.STROKE);
Path path = new Path();
path.moveTo(myPath[0].x, myPath[0].y);
for (int i = 1; i < myPath.length; i++){
path.lineTo(myPath[i].x, myPath[i].y);
}
path.close();
canvas.drawPath(path, paint);

use this.maybe helpful but you need to customize on your own

Upvotes: 0

Trevor
Trevor

Reputation: 10993

This is a very broad question. My answer is just going to quickly run though how I would personally do the speech bubbles in terms of Views.

On the Android platform, representing any kind of information on the screen is usually done using a mixture of View objects. For example, if you display an image, you use an ImageView. To display a list of items, it's a ListView. There are many, many standard types of View, for different purposes, that the system has as standard for displaying different kinds of information. You would typically build up your UI screen using a combination of such Views.

Quite often, you need to create your own View subclass. You might typically do this if you want to draw some graphics of some kind, i.e. when you're drawing vector graphics to a Canvas. For example, if you wanted to draw your speech bubble using line Path() objects (e.g. draw an ellipse and lines, with a black stroke and white fill, to form the speech bubble) you might create SpeechBubbleView, which extends View. In its onDraw() method you'd draw the speech bubble graphics. On the other hand, you might want to represent your speech bubble using a bitmap instead of drawing it. And then, whichever way you draw the speech bubble, you'll want to insert text inside it - possibly using a TextView. But wouldn't it be nicer to represent the bubble and text within it as a single View object? Yes, so what you might possibly do is contain your speech bubble graphic and TextView within a ViewGroup -- a ViewGroup is a type of View that can hold child Views. The specific subclass of ViewGroup you'd possibly use is RelativeLayout, which is a View that allows its child Views to be overlapped. So, you'd have SpeechBubbleView that extends RelativeLayout, and the child views of that might be a TextView and the ImageView containing the speech bubble bitmap.

As for how to overlay your speech bubbles over the image, once again you would probably use RelativeLayout. In your layout XML file the main parent container would be a RelativeLayout. Within that one child View would be perhaps an ImageView that contains the bitmap that the user wants to place speech bubbles over. Then, further child Views would be your SpeechBubbleViews.

As for dragging the speech bubbles around, you might do this by using OnTouchListener in your Activity. I've got some drag-and-drop code working in a current project I have open right now; I could potentially copy and paste some of it in for you, but to be honest I think this is something for a separate, more specific question and you need to get to grips with more of the basics first.

Your question is a very broad one and there is a lot of ground for you to cover, but hopefully these pointers will be useful.

Upvotes: 3

dmon
dmon

Reputation: 30168

I think the easiest (note: not necessarily the best) way to implement this would be to use a 9 patch speech bubble image and just set it as the background for a TextView. You can add text and everything should be resized just automatically if you built the 9-patch right. Unfortunately, I don't have much experience with drag and drop in Android, but it shouldn't be too hard.

Upvotes: 0

Related Questions