FrameLayout borders in Android app - android

What I have to implement in order to limit the max size of a FrameLayout or the View inside it?
I am using a FrameLayout for my Android app:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="#+id/dog_holder"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
I have added a View to it:
FrameLayout frame = (FrameLayout) findViewById(R.id.dog_holder);
dog = new DogView(this);
frame.addView(dog);
(DogView extends View)
I am initializing the View with a bitmap:
my_dog = BitmapFactory.decodeResource(getResources(), R.drawable.dog);
I want to move around the picture (I have implemented the touch controls) but not to be able to move outside it. Good example is a picture gallery app: while a picture is zoomed you can go around it but on the borders there is bouncy effect which doesn't allow to go into the dark side.
What I have to implement to stop me when I try to go outside the dimension of the bitmap?

Related

add buttons in front of javacameraview Android

I have a javacameraview in my main activity. I would like to put some control buttons in front of the camera
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.gustavo.tassio.opencvnativeandroid.Principal">
<org.opencv.android.JavaCameraView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
app:show_fps="true"
app:camera_id="any"
android:id="#+id/cameraOpenCv"/>
JavaCameraView cameraView;
cameraView = (JavaCameraView) findViewById(R.id.cameraOpenCv);
cameraView.setVisibility(SurfaceView.VISIBLE);
cameraView.setCvCameraViewListener(this);
it is like this now, just the pure image, my idea is to put the buttons on this scene
https://i.stack.imgur.com/9YKyP.jpg
If you add buttons to your framelayout, they will be displayed as you wish. The views in framelayout are displayed in chronological order. When you change visibility of a subview of a framelayout, it's order does not change, all subviews that you added after it either in xll or programmatically will be drawn "above".

android - how to create footer banners like admob

I want to add my custom banners and gif animations in some of my activities like the way admob did .
I was wondering how can I do some thing like admob but put my own banners ?
Create a fragment and attach it to the bottom of the activity in which you want to display the ads. You will have to put an ImageView/TextView inside the fragment and do the other necessary things, like downloading the Image from the server...
You can add a LinearLayout to the activity's xml layout and place it at the bottom.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Add this container layout to the bottom of the screen -->
<LinearLayout
android:id="#+id/ad_container"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
And set up the custom gif image to the adView like the following way..
public void initAdView() {
LinearLayout adViewContainer = findViewById(R.id.ad_container);
ImageView adView = new ImageView(activity);
adViewContainer.addView(adView);
//make other thing to set the gif image to the imageView.
adView.setImageDrawable(gifImage);
}

Set background on SurfaceView

I’m developing an Android drawing app, using a SurfaceView (to be more specific it’s a class that extends SurfaceView). I already saw a lot of topics on this forum, but I didn’t get my answer.
My activity is basically a FrameLayout, which contains all of my views. I would like to set my SurfaceView on the lowest level of my FrameLayout, in order to see the upper elements (buttons, fragments, etc.). I also would like to set the background of my SurfaceView with a drawable but I’m facing problems.
I tried first to set the background of my SurfaceView itself and it works. Unfortunately, my painting content (canvas and bitmap) was overload by this background so I tried a second solution. I apply the drawable background to my FrameLayout, and I set my SurfaceView background as transparent, using setZOrderOnTop. My SurfaceView was indeed transparent, but my drawing stuff was above my buttons and fragments.
So my first question is: why do we need to setZOrderOnTop to get a transparent background? Then, how can I set a simple drawable background, keeping my structure hierarchy?
Thank’s for your answers!
XML View:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/mainFrame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/repeat_background"
tools:context=".MyActivity">
<!-- Option menu -->
<fragment ... />
<Button ... />
<Button ... />
<Button ... />
...
</FrameLayout>
#drawable/repeat_background
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/drawing_font_texture"
android:tileMode="repeat" />
MyActivity
#Override
protected void onCreate(Bundle p_savedInstanceState) {
super.onCreate(p_savedInstanceState);
setContentView(R.layout.main_controller_activity);
// Getting my FrameLayout
FrameLayout mainFrame = (FrameLayout) findViewById(R.id.mainFrame);
// Instantiating my SurfaceView
this.drawableView = new MCustomDrawableView(getApplicationContext());
// Don't works
//setZOrderMediaOverlay(true);
this.drawableView.setZOrderOnTop(true);
this.drawableView.getHolder().setFormat(PixelFormat.TRANSLUCENT);
FrameLayout.LayoutParams style = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT);
// Add the SurfaceView to the FrameLayout, on lowest position
mainFrame.addView(this.drawableView, 0, style);
}
I think your solution is on the right way. Set the background to the framelayout or add another layout under the surfaceview for your drawable. Then increase the z indices of the surfaceview AND the z indices of the buttons/elements that should be over the surfaceview,otherwise they are under it.
If you use material design, you can also set the elevation in dp for nice shadows.

Android - canvas onDraw does not fire

I am trying to add a canvas view to a relative layout inside a horizontal scroll view.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#fff"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/hsv"
android:layout_width="fill_parent"
android:background="#EEDB00"
android:layout_height="30mm">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/canvas"
android:background="#000"
android:layout_width="300mm"
android:layout_height="20mm">
</RelativeLayout>
</HorizontalScrollView>
</RelativeLayout>
I created a class called CanvasView which extends View, and I drew some basic shapes by over-riding onDraw(). However the canvas does not appear in the relativelayout, when I do:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
cView = new CanvasView(this);
rLayout = (RelativeLayout)this.findViewById(R.id.canvas);
rLayout.addView(cView);
}
However, when I directly add it by calling setContentView(cView); it works. On digging, I found that when I call addView(), the onDraw() is not firing at all, and hence the canvas is not drawn... Any ideas on how to fix this?
I'm not in a position to test this myself at the moment, but I think your problem is that you're not applying any LayoutParams to the View, which may mean it occupies no screen space. If a View is ever off-screen or completely obscured or the system otherwise decides that nothing it draws will be visible, then I believe onDraw() won't be called at all.
Try setting some width and height to your View when you add it to your RelativeLayout:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(50,50);
rLayout.addView(cView, params);

How to overlay an icon image on top of an exisiting view

This is a two part question.
I have an image of a warehouse that I would like to divide it into regions (A,B,C,D,E & F) where each letter represents a storage in the warehouse. If the user selects storage "B" then I would like to programmatically overlay an icon over the region on the images that is designated for "B".
Question:
What is a good way to subdivide the image into regions that will describe each of the storage room?
How to programmatically place an icon over the correct region?
Thank you.
Answer to 1: You can use Framelayout; FrameLayout is the general mechanism for overlaying a view on top of another.
Here's an example:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/my_image"/>
<View
android:id="#+id/overlay"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</FrameLayout>
Then in your Java code you can dynamically set the transparency of your overlay:
View overlay = (View) findViewById(R.id.overlay);
int opacity = 200; // from 0 to 255
overlay.setBackgroundColor(opacity * 0x1000000); // black with a variable alpha
FrameLayout.LayoutParams params =
new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, 100);
params.gravity = Gravity.BOTTOM;
overlay.setLayoutParams(params);
overlay.invalidate(); // update the view
See here
Question 2: In framelayout, you can set the icons on top of where you want by dragging them:: So simple!
Hope this helped:: XD

Categories

Resources