How to draw ImageView on ImageView? - android

I have an ImageView and I would like to draw dinamically another images on my base ImageView.
In this image for example I have a tree and I would like to draw image of people
Any suggestions to start?
yes my problem is like #Tsunaze say:
I have a tree, and I want to put heads on it,

XML :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/iv_tree"
android:scaleType="fitXY"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:src="#drawable/my_tree" />
</RelativeLayout>
MyView.java :
public MyView extends View{
private Bitmap head_1, head_2;
public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.context = context;
init();
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
init();
}
public MyView(Context context) {
super(context);
this.context = context;
init();
}
private void init(){
head_1= BitmapFactory.decodeResource(context.getResources(),
R.drawable.head_1);
head_2= BitmapFactory.decodeResource(context.getResources(),
R.drawable.head_2);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(head_1, coordinateX_head_1, coordinateY_head_1, null);
canvas.drawBitmap(head_2, coordinateX_head_2, coordinateY_head_2, null);
}
}
You choose the coordinate you want, you can even use function like canvas.getWidth() or canvas.getHeight(). Pose yourself as a painter. Good luck.

What you have to do is create a class that extends from ImageView, set the default parameters that you want as usual and override the on draw method, this method will give you the chance to get a reference to the canvas and draw bitmaps or pretty much anything you want, but remember to call first super.onDraw to keep your default configuration and overlay your new images.
There's plenty of tutorials on how to use canvas, so now you have the proper path to take, hope this helps.
Regards

setBackgroundResource to ImageView and use setImageResource over it.
Ex :
imageView.setBackgroundResource(R.id.backgroundTree);
imageView.setImageResource(R.id.allPeople);
EDIT :
If you want multiple images to be overlayed, check this example.
You can also use RelativeLayout and add ImageView inside it programmatically.
<RelativeLayout>
<ImageView/>
<ImageView/>
<ImageView/>
<ImageView/>
</RelativeLayout>

Related

How to extend ImageView

I need to create my own ImageView.
This is my class:
public class Brick extends ImageView implements Serializable{
public Brick(Context context) {
super(context);
}
public Brick(Context context, AttributeSet attrs) {
super(context, attrs);
}
public Brick(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
}
When I try to use my ImageView inside a xml layout file (as you can see below) I have a problem. I can see a black shape, but there is not the image (the drawable called d) inside it.
<com.myapp.Brick
android:id="#+id/myBrick"
app:srcCompat="#drawable/d"
android:background="#000000"
android:layout_width="300dp"
android:layout_height="300dp" />
What's my error?
You should call android:src="#drawable in your XML Section .
<com.myapp.Brick
android:id="#+id/myBrick"
app:srcCompat="#drawable/d"
android:background="#000000" // showing Black Shape Background
android:layout_width="300dp"
android:layout_height="300dp"
android:src="#drawable/add_image /> // Add android:src
You should add app:srcCompat or android:src in your XML Section

Android: best way to control view that added dynamically?

As the title, i want to know if there is a the best way to control a view that added dynamically. (we have to keep reference to the view that was added)
Some time, for a complex request we have to add view in runtime. The is some ways to do that. In my case:
Some time i use a listview/recyclerview and control view via the list/recycleview adapter.
Other way is use a hashmap.
Do you have any other ideas? and how it work?
I prefer way, when I define View both by Java and XML file. View created like this, gives you ability to call your own Java methods, but you don`t need to create whole layout dynamically in Java. Little example:
MyView.java:
public class MyView extends LinearLayout {
TextView textView;
public MyView(Context context) {
super(context);
init();
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public void init() {
inflate(getContext(), R.layout.my_view, this);
setOrientation(VERTICAL);
textView = (TextView) findViewById(R.id.text_view);
}
public MyView setContent(String value) {
textView.setText(value);
return this;
}
}
my_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="#+id/text_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</merge>
After that, you can simply add it in you layout through Java:
cont.addView(new MyView(this).setContent("Value"));
or xml:
<com.path.to.your.view.MyView
android:id="#+id/my_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

BitmapDrawable cannot be cast to RoundRectDrawableWithShadow

I am trying to extend a cardview to set the background image. I know that this can not be done with normal cardview. I have searched net and found plenty of solutions for setting a background color to the card view but none for image.
My code to do so:
public class CCView extends CardView {
public CCView (Context context) {
super(context);
init();
}
public CCView (Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CCView (Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
setBackground(getResources().getDrawable(R.drawable.cc_background));
}
}
I get this exception when I populate the code from XML
android.graphics.drawable.BitmapDrawable cannot be cast to android.support.v7.widget.RoundRectDrawableWithShadow
Any solution?
As CardView extends FrameLayout you can layer layouts on top of it. To get around the problem you're having, I'd try adding a blank view "underneath" all the other elements in your view, and then set that view to inherit the state of its parent. Something like this:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent"
android:layout_width="match_parent"
card_view:cardBackgroundColor="#DDFFFFFF"
card_view:cardElevation="#dimen/card_elevation">
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:duplicateParentState="true"
android:background="#drawable/card_background"/>
<LinearLayout
....
....
....
/LinearLayout>
</android.support.v7.widget.CardView>

how to put a custom view inside of a custom viewGroup / Layout

custom view inside of a custom viewGroup not visible, how can I get it to show up?
or is there a better way to do this that would work?
no compile or runtime errors but the view does not show up in the viewGroup, it is supposed to fill the area with color like the other views but it is white and the color for the view is not showing up inside of the CustomLayout
xml code, the first 2 views show up with no problems but the 3rd view that is nested inside of the CustomLayout does not show up, just white color area, the view inside not visible
CustomViewOne is a separate class file, CustomViewTwo and CustomViewThree are both nested inside the MainActivity class as static inner classes, and CustomLayout is a separate file
<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"
tools:context=".MainActivity" >
<com.example.customviewexample.CustomViewOne
android:layout_width="100dp"
android:layout_height="50dp" />
<view
class="com.example.customviewexample.MainActivity$CustomViewTwo"
android:layout_width="100dp"
android:layout_height="50dp" />
<com.example.customviewexample.CustomLayout
android:layout_width="100dp"
android:layout_height="50dp">
<view
class="com.example.customviewexample.MainActivity$CustomViewThree"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</com.example.customviewexample.CustomLayout>
</LinearLayout>
here is the code for the CustomViewThree, vary simple like the other custom Views it just fills the area with color, it is nested inside of the MainActivity so you have to use MainActivity$CustomViewThree to access it.
public static class CustomViewThree extends View {
public CustomViewThree(Context context) {
super(context);
}
public CustomViewThree(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomViewThree(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.GREEN);
}
}
and here is the code for the CustomLayout class
public class CustomLayout extends FrameLayout {
public CustomLayout(Context context) {
super(context);
init(context);
}
public CustomLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public CustomLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
public void init(Context context) {
}
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
}
}
custom view inside of a custom viewGroup not visible, how can I get it
to show up?
Your parent CustomLayout which wraps the child has a empty onLayout() method which makes the child to not appear. This method is important in a ViewGroup because it's used by the widget to place its children in it. So, you need to provide an implementation for this method to place the children(by calling the layout() method on each of them with the proper positions). As CustomLayout extends FrameLayout you could just call the super method to use FrameLayout's implementation or even better remove the overridden method(is there a reason for implementing it?).

Extending View class and layouts in Android

I am playing with layouts and views in Android. My layout looks like:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.mycode.MyView
android:id="#+id/MyView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<LinearLayout
android:id="#+id/leftpane"
android:layout_width="55dp"
android:layout_height="wrap_content"
android:layout_weight="0.95"
android:background="#color/leftpane_background"
android:gravity="center"
android:orientation="vertical"
android:scrollbarStyle="insideOverlay" >
</LinearLayout>
</LinearLayout>
If i comment the lines <com.mycode.MyView .... />, the left linear layout (with id leftpane) is showed, otherwise not. If i add another layout after the leftpane layout (for example a scroll view layout), it does not get showed as well.
Why using my custom view (MyView) makes disappear all other layouts?
What i would like to do is to have my custom View for the background of the application, where i would show images, and some other view over the background, like scroll views, buttons and so on.
This is the code of MyView class. Keep in mind that i need to draw on the background of the activity an images (on the whole background, obviously not including the action bar):
/**
*/
class MyView extends View
{
TextPaint text_paint;
Paint paint;
private void InitView()
{
text_paint = new TextPaint( Paint.ANTI_ALIAS_FLAG );
text_paint.setColor( Color.BLACK );
paint = new Paint();
}
public MyView(Context context)
{
super(context);
InitView();
}
public MyView(Context context, AttributeSet attrs)
{
super(context, attrs);
InitView();
}
public MyView(Context context, AttributeSet attrs, int defaultStyle)
{
super(context, attrs, defaultStyle);
InitView();
}
#Override
protected void onDraw( Canvas canvas )
{
super.onDraw(canvas);
//int w, h;
//w = canvas.getWidth();
//h = canvas.getHeight();
//paint.setColor( Color.WHITE );
//canvas.drawRect(0, 0, w-1, h-1, paint );
//canvas.drawText( "ciao", 100, 100, text_paint);
}
};
Try to change your view's height as:
android:layout_height="wrap_content"
Maybe you did some error work when you overrode the onMeasure method in your custom MyView,
and so the MyView's match all the parent view.
Can you show your code which in the onLayout and onMeasure method of the MyView.

Categories

Resources