How can I create an ImageView programmatically and draw on canvas in Android?
I also saw this post of-canvas and tried the below code:
ImageView img = new ImageView(HighScore.ctx);
img.setImageResource(R.drawable.interactive);
img.draw(canvas);
img.bringToFront();
img.invalidate();
img.draw(canvas);
But the answer doesn't seem complete, it's not working. plz can anybody help me..
Refer to this link: http://developer.android.com/reference/android/widget/ImageView.html#lattrs
Here equivalent methods of xml attributes for imageview are given.
Related
I was going through Android : Converting color image to grayscale and other similar answers, when I doubted whether it's possible to grayscale a coloured "background image" i.e., image set using android:background="". Is it possible? If yes, how?
I have not got the answer even after several hours of extensive googling.
EDIT: I have found a hack for the problem, but it does not solve the problem completely. I have used
android:backgroundTint="#999999"
android:backgroundTintMode="multiply"
along with my code to set the background image.
Use an ImageView in your layout instead of trying to set the background to a drawable and then use this code to programmatically color the image to grayscale.
final ColorMatrix matrix = new ColorMatrix();
matrix.setSaturation(0);
final ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
mImageView.setColorFilter(filter);
If you need help using an ImageView instead of background, please post your layout xml and java code for this scene.
I'm taking screenshots using the code below.
Inside the layout I am using a custom drawable. The problem is that the bitmap shows everything as expected, except the custom drawable which looks kind of messed up.
I know the problem is related to the drawable. but I don't know what it is.
` View view = app.getCurrentActivity().getWindow().getDecorView().getRootView();
view.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);`
Thank you for your help, Niv
ok so i figure it out.
the problem was that inside the onDraw in the custom drawable i used the 'canvas.getWidth()' instead of 'getBounds().width();'
I have a relative layout in my app which im trying to take screenshot of. The problem is that it has CircularImageView in it (https://github.com/lopspower/CircularImageView) and the whole image is transformed and no longer cropped to center.
The code goes like this:
View rl = findViewById(R.id.toBeScreenShot);
rl.setDrawingCacheEnabled(true);
Bitmap screenshot = Bitmap.createBitmap(rl.getWidth(),rl.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(screenshot);
rl.draw(c);
rl.setDrawingCacheEnabled(false);
And this is the result:
The XML code is too big to share, there is a Relative layout that im taking screenshot of (in code, its 'rl'). Then there is a FrameLayout, then two LinearLayouts, some ImageViews and then there is the CircularImageView:
<com.mikhaellopez.circularimageview.CircularImageView
android:id="#+id/userPicture"
android:layout_width="#dimen/_120sdp"
android:layout_height="#dimen/_130sdp"
android:layout_gravity="center"
app:civ_border="false" />
Any idea what might cause this thing to happen?
So far I've been able to figure out that the bug must have something to do with the library that I'm using. So I tried to figure out some other way to create circular image, and I found this answer from Waza_Be:
https://stackoverflow.com/a/14180142/6148510
After some short testing, the screenshot of this circular bitmap works properly. If there will be no answer to my problem, I will mark this workaround as a correct answer.
Now I want to insert a ImageView behind the GLSurfaceView.But the GLSurfaceView always draws background color so I can't see the ImageView.I tried glClearColor(0,0,0,0) but it doesn't work.What should I do?Please help me!
Thank you very much.
If I remember correctly you should be able to adapt this
Overlapping Views in Android
to your needs
(relative layout and android:background="#0000")
since GLSurfaceView is a View it should work.
I fixed it.
using
gLSurfaceView.setEGLConfigChooser(8,8,8,8,16,0);
gLSurfaceView.setRenderer(this);
RelativeLayout rl = new RelativeLayout(this);
BgLayout bgl = new BgLayout(this);
gLSurfaceView.getHolder().setFormat(PixelFormat.TRANSLUCENT);
gLSurfaceView.setZOrderOnTop(true);
rl.addView(bgl);
rl.addView(gLSurfaceView);
this problem has been bothering me for days and I cannot figure out why on earth it is happening. I have a method that detects for a face, if a face is detected, the method will draw a rectangle on a canvas along the face.
That part works fine.
The problem is, when it displays to the image_view in my xml file, it will display on the far left middle of the image view, in a box, rather than in the center with the width as fill_parent.
I thought of a possible workaround : to set the bitmap as a bitmapdrawable, but canvas only draws to bitmaps, so I can get the bitmapdrawable in the middle, but with no red box around it.
I commented out my testing of bitmap drawable, and left it as what I have now...it only displays to my xml in the far left of the image_view. Please take a look and help!
I posted to pastebin because the code is a bit long (I am also using a linear layout within a view flipper).
method:
http://pastebin.com/KQ5Pmx45
xml:
http://pastebin.com/jbt3j40h
Since I am able to get the faces to detect, and I can paste a bitmap drawable to an imagebutton and it centers just fine, is there a way to get the rectangle around the bitmapdrawable if my problem cannot be solved?
Thanks!
EDIT: I tried to implement it into a RelativeLayout and still doing the same thing:
http://pastebin.com/daqMdXjB
Could this code be getting my way of centering?
public void processCameraImage(Intent intent) {
mFlipper.setDisplayedChild(1); // 1 == your second layout
mThePicture = (ImageView) findViewById(R.id.image_view); //
cameraBitmap = (Bitmap) intent.getExtras().get("data");
mThePicture.setImageBitmap(cameraBitmap);
detectFaces();
}
Use on Imageview tag android:scaleType="fitCenter" or android:scaleType="centerInside" or android:scaleType="center" sclaetype attr doc
I hope, I understood your problem right.