I have a frameLayout in xml:
<FrameLayout
android:layout_width="150px"
android:layout_height="200px"
android:id="#+id/preview">
</FrameLayout>
This preview is for displaying camera view:
mPreview = new CameraSurfacePreview(MainActivity.this, cameraObj,...);
preview.addView(mPreview2);
....
It successfully displaying face from the front camera. And I have the face rectangle x and y coordinates. How can I display a bounding box of face on the frameLayout?
Thank you.
well, you could use a ShapeDrawable, and set it's layout parameters to be the size and location you need, and add it to the FrameLayout along with your CameraSurfacePreview.
It's not all the difficult. First create a ShapeDrawable with the properties you want. Then set it as the background of a standard View object, and add the view with the layout parameters to size it how you want. So if you want the
ShapeDrawable sd = new ShapeDrawable(new RectShape());
sd.getPaint().setColor(0xFFFFFFFF);
sd.getPaint().setStyle(Paint.Style.STROKE);
sd.getPaint().setStrokeWidth(1);
View shapeView = new View(context);
shapeView.setBackground(sd);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(100, 100);
params.setMargins(left, top, 0, 0);
frameLayout.addView(shapeView, params);
In this particular case, i've made it a 100x100 view, so the shape will auto size to the view. and i've set it up so that it's offset from the top corner by the values left and top.
There's lots of ways to do this, but this seems the simplest. Of course you could do all this in XML too. There's lots of tutorials out there on how to do this.
Related
first I thought this is a trival thing but when I've started to code I found that it is not so easy. So let me start. I have a SurfaceView in which I'm displaying preview from my camera On top of that I want to put an ImageView and move it from max left border to max right border.
<SurfaceView
android:id="#+id/cameraPreview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
I put my ImageView from my activity
Drawable drawable = getResources().getDrawable(R.drawable.img_compass, this.getTheme());
objectImage = new ImageView(this);
objectImage.setVisibility(View.VISIBLE);
objectImage.setImageDrawable(drawable);
The first problem that I see is the fact that my image is not set to the left border. It is a bit to the center. So I've tried to set the margin using LinearLayout.LayoutParams but it didn't help. Same efect.
WHen I set my animation
TranslateAnimation fromLeftToRight = new TranslateAnimation(0, 200, 0, 0);
It is not starting from max left border but from the position that is on the start.
So my 2 questions are:
how from code I can set my image to the left border of my surfaceview ?
if left border is represented by 0 how can I get position of right border ?
i have a view in linear layout. the app is music notation which is a series of musical bars which line wrap so they always less than width of screen (no horizontal scrolling). there is a vertical scrollbar when the music score gets too big for the view. so far so good. i can scroll up and down fine.
now i'm implementing pinch zoom, i set up an onScale in a ScaleGestureDetector, save the scale factor, invalidate and in onDraw i use the scale factor like this canvas.scale(mScaleFactor, mScaleFactor);
it scales the view ok except its not doing exactly what i want. i want the drawing area of the canvas to scale as it does but the scrollbar to stay in place, but zooming sends it off to the side, out of view.
any idea how to scale the view but not the scrollbar?
BTW, i can recalculate where to line wrap my music bars by applying the scalefactor to the width, its really just a problem with the scrollbar display.
can anyone help? thanks
I figured out the way. I needed to decouple the drawing of the scrollbar from the drawing of the main music notation view. that way I can zoom the main notation view without affecting the scroll bar.
so I use linear layout to create 2 views -
layout.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT,
100));
layout.setOrientation(LinearLayout.HORIZONTAL);
Globals.mMainView = new MainView(context);
Globals.mMainView.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 60));
layout.addView(Globals.mMainView);
Globals.mScrollbarView = new ScrollbarView(context);
Globals.mScrollbarView.setLayoutParams(new LinearLayout.LayoutParams(20, LinearLayout.LayoutParams.WRAP_CONTENT, 0));
layout.addView(Globals.mScrollbarView);
then I remove display of scrollbars from MainView and put them in ScrollbarView.
then I use onScroll events in ScrollbarView to control scrolling in MainView. I have to remember to return the actual display length of MainView from ScrollbarView.computeVerticalScrollRange so the scroll bar size shows up ok.
since I have reference to both views saved as globals, its easy for them to talk to each other as necessary.
I have a big image with transparent circle in the center. I want to overlap this over a MapView so that you get to see the map in a circle. The image is bigger than any possible device screen so that it will look basically the same on all sizes. The rounded corners in the image are complex and that's why I need to do this.
How can I position the ImageView so that parts of it are offscreen?
Make the transparent circle a ninepatch, for example: http://i.imgur.com/8wmrQ.png
Save it into res/drawable-hdpi/mask.9.png
Than you can use it as
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout rl = new RelativeLayout(this);
MapView mapView = new MapView(this, "apikey");
mapView.setClickable(true);
rl.addView(mapView);
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.drawable.mask);
imageView.setScaleType(ScaleType.FIT_XY);
rl.addView(imageView, RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
setContentView(rl);
}
I suspect that what you want to achive is not exactly what you describe. However, if that is the case the solution posted by #Daniel Fekete is the best approach. To that solution, I just add, that you can also define the ImageView in the same layout file where you are defining the MapView, instead of adding the view programmatically. Of course the ImageView should be defined after the MapView to have it being drawn over the map.
If you need a more flexible solution, where the ImageView can move aligned with the map, or having it covering the map, but not covering other overlays items that you add to the map, then you can use the the following approach:
Create an overlay that mask (cover) the part of the map that you want to cover. In the onDraw() of this oevrlay just draw something that achive your mask requirements (shapes, colors and opacity).
If you want to have other overlay items visible, you need to add the overlay with these items after you add the overlay that mask map to the mapView.getOverlays.add().
Enjoy it.
I have a class that extends View that I draw into.
In spite of the fact that the view is set up as a child to a RelativeLayout that has specific dimensions, the View's canvas reports that it is 1280 wide (which is as wide as the device).
I'm trying to understand what I need to do to have the canvas be constrained by the dimensions of its View (as common sense would dictate).
Curious what I'm overlooking... here are the relevant code snippets...
dv = new DrawView(this, getWindowManager());
dv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
//this wrapper is 500x500
((RelativeLayout)findViewById(R.id.drawWrapper)).addView(dv);
L.log("DRAWVIEW WIDTH = " + dv.getWidth()); //this reports 0... why?
inside DrawView...
#Override
public void onDraw(Canvas canvas) {
L.log("CANVAS WIDTH ON DRAW = " + canvas.getWidth()); //this always reports 1280 on xoom
this.canvas = canvas;
drawElementsOntoCanvas(canvas, true, false);
...
}
Is there something I'm overlooking or is the canvas inside a View always the width and height of the entire screen?
The XML for the relative layout:
<RelativeLayout
android:id="#+id/drawWrapper"
android:layout_width="500px"
android:layout_height="500px"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dip"
android:clipChildren="true"
android:background="#drawable/cut_out_frame"
/>
Ah, now I see it :)
You have clipChildren = true but this applies to the containing layout. Child views can draw outside the bounds of their parent but the drawing will simply be clipped to the parent dimensions.
It seems that here, you are using the window manager to construct your DrawView
dv = new DrawView(this, getWindowManager());
dv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
Without seeing the constructor, I am guessing that the subsequent setLayoutParams is using FILL_PARENT, i.e. the entire screen.
If I were you, I would implement the standard constructors for your DrawView and then lay it out in the layout XML. This is the "Android" way. You can also override onMeasure() to track how big DrawView is, storing the x and y in internal fields, which you can use to do any scaling you need. That way, if your layout every changes (orientation change, different resolution support, new version) your code doesn't need to change.
Finally, never (unless you REALL know why) use PX in layouts. Use DIP instead.
i need to draw two imageviews, (+) and (-) symbols, on the bottom right corner of the screen, similar of the zoom objects from googlemaps.
I need to do it programatically, with Java, and without using XML files.
I'm trying to do with relativelayout, but i dont know how to do it. They must to be on the bottom right corner of the screen, with 5 or 10 pixels of separation between them.
How to do it?
Also will be cool if someone can tell me how to detect when a user has pressed each image.
You can use gravity.
http://developer.android.com/reference/android/widget/RelativeLayout.html#setGravity(int)
this might do it:
image.setGravity(Gravity.RIGHT | Gravity.BOTTOM);
This should work
ImageView plusImage = new ImageView(this);
RelativeLayout.LayoutParams pp = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
pp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,RelativeLayout.TRUE);
pp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,RelativeLayout.TRUE);
pp.leftMargin = 5;
plusImage.setId(501);
plusImage.setLayoutParams(pp);
plusImage.setImageResource(R.drawable.icon);
ImageView minusImage = new ImageView(this);
RelativeLayout.LayoutParams mp = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
mp.addRule(RelativeLayout.LEFT_OF,plusImage.getId());
mp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,RelativeLayout.TRUE);
mp.rightMargin = 5;
minusImage.setId(502);
minusImage.setLayoutParams(mp);
minusImage.setImageResource(R.drawable.icon);
//Add the images to the outer layout
((RelativeLayout)findViewById(R.id.outerlayout)).addView(plusImage);
((RelativeLayout)findViewById(R.id.outerlayout)).addView(minusImage);