Get SVG out of ImageView - android

I'm using AndroidSVG to dynamically place a signature rendered as an svg, for displaying to an ImageView.
Here is my code:
SVG svg = SVG.getFromString(singleEmployee.signature);
ImageView image_view = (ImageView) layout_signature.findViewById(R.id.toolboxtalk_signature_svg);
image_view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
image_view.setTag(String.format(Locale.US, "employee_signature%d", x));
Drawable drawable = new PictureDrawable(svg.renderToPicture());
image_view.setImageDrawable(drawable);
My question is, how do I get the SVG out of the ImageView later in my code execution.
Here is what I'm trying:
ViewGroup v1 = (ViewGroup) findViewById(R.id.job_ticket_form);
ImageView signatureImageView = (ImageView) v1.findViewWithTag("employee_signature1");
PictureDrawable pictureDrawable = (PictureDrawable) signatureImageView.getDrawable();
Picture picture = pictureDrawable.getPicture();
Context context = signatureImageView.getContext();
String employeeSigSVGString = null;
try {
SVG svg = SVG.getFromResource(signatureImageView.getContext(), R.id.toolboxtalk_signature_svg);
employeeSigSVGString = spinner_value + "|" + svg;
} catch (SVGParseException e) {
e.printStackTrace();
}
Any ideas how to get the SVG back out of the ImageView?

Related

How to get all images as array of bitmap from drawable folder using Drawable?

I am using the below code to get 9 (bird1 to bird9) images from drawable folder by manually giving each name.
How can i use an array or replace R.drawable.bird1 with variable ( as the below statement accept only actual value) to get all the 9 images in an array?
Drawable myDrawable = ResourcesCompat.getDrawable(getApplicationContext().getResources(), R.drawable.bird1, null);
bitmap1 = ((BitmapDrawable) myDrawable).getBitmap();
myDrawable = ResourcesCompat.getDrawable(getApplicationContext().getResources(), R.drawable.bird2, null);
bitmap2 = ((BitmapDrawable) myDrawable).getBitmap();
I got it working like this. I'm sure someone can do it much nicer... but this works.
Caveats:
1) Remember to replace "com.example" with your real package name.
2) Replace "numberOfBirdsBitmaps = 3" with however many you have.
3) The "derp" ImageViews are just for my testing purposes.
public class MainActivity extends AppCompatActivity {
int numberOfBirdsBitmaps = 3;
ArrayList<Bitmap> birdBitmapArray = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView derp1 = (ImageView) findViewById(R.id.imageView);
ImageView derp2 = (ImageView) findViewById(R.id.imageView2);
ImageView derp3 = (ImageView) findViewById(R.id.imageView3);
for (int i = 0; i<numberOfBirdsBitmaps; i++) {
String temporaryString = "bird" + ((Integer) (i+1)).toString();
int temporaryIdentifier = getResources().getIdentifier(temporaryString, "drawable","com.example");
Drawable temporaryDrawable = ResourcesCompat.getDrawable(getApplicationContext().getResources(), temporaryIdentifier, null);
Bitmap temporaryBitmap = ((BitmapDrawable) temporaryDrawable).getBitmap();
birdBitmapArray.add(temporaryBitmap);
}
derp1.setImageBitmap(birdBitmapArray.get(0));
derp2.setImageBitmap(birdBitmapArray.get(1));
derp3.setImageBitmap(birdBitmapArray.get(2));
}
}
Love,
Boober.
You can try and get the drawable id using a loop:
for(int=1; i<=9 ; i++)
{
getResources().getIdentifier("bird"+i,"drawable",getPackageName());
}

How to set position of Fast Scroll?

I'm now trying to make a ListView with a fast scroll on it.
And I made a custom fast scroll thumb with the code following:
try {
Field f = AbsListView.class.getDeclaredField("mFastScroller");
f.setAccessible(true);
Object o = f.get(listview);
f = f.getType().getDeclaredField("mThumbImage");
f.setAccessible(true);
ImageView iv = (ImageView) f.get(o);
iv.setImageDrawable(drawable);
// Drawable drawable = (Drawable) f.get(o);
// drawable =
// getResources().getDrawable(R.drawable.scroll_selector);
// f.set(o, drawable);
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
Although it shows image perfectly, it has an invisible area that occupies too much part of ListView row. Can anyone give me a suggestion what I should do right now? I want the scroll to be placed at the end of the right edge..

how to programmatically add an image to a activity android

Hi I am trying to programatically add an image to an activity for an android app
I have this :
for (int i = 0; i < num_devices; i++) {
ImageView imageView = new ImageView(this);
LinearLayout.LayoutParams vp = new LinearLayout.LayoutParams
(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
imageView.setLayoutParams(vp);
try {
Class res = R.drawable.class;
Field field = res.getField(device_types.get(i));
int resId = field.getInt(null);
imageView = (ImageView) findViewById(resId);
}
catch (Exception e) {
Log.e("MyTag", "Failure to get drawable id.", e);
}
LinearLayout link_devices = (LinearLayout) findViewById(R.id.link_devices);
link_devices.addView(imageView);
however it doesnt let me get the location of the images ( i try and get 0 for getTop, getLeft etc..)
am i doing it wrong and was is the correct way to do it
You are not setting image on that imageview, and you have set WRAP_CONTENT as layout params, which means the size of the imageView is same as size of image you are setting on it.
Since no image is attached the imageView size is 0.
Try setting an image, using any one of the codes ;- imageView.setImageBitmap(Bitmap bm), setImageDrawable(Drawable drawable) or setImageResource(int ResID).
I don't get what you're doing the with this code & its not required:
Class res = R.drawable.class;
Field field = res.getField(device_types.get(i));
int resId = field.getInt(null);
imageView = (ImageView) findViewById(resId);

Android, How to add a bitmap layer over another bitmap layer, dynamically from list?

In my application, I have a list adapter. each row includes an imageView in left and two textViews in right. For getting images I'm using Universal-Image-Loader library.
my getView() method is this:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = myInflater.inflate(R.layout.list_news_adapter, null);
holder = new ViewHolder();
holder.ivIcon = (ImageView) convertView.findViewById(R.id.list_news_icon);
holder.tvTitle = (TextView) convertView.findViewById(R.id.list_news_title);
holder.tvDate = (TextView) convertView.findViewById(R.id.list_news_date);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tvTitle.setText(video.getTitle().get(position));
holder.tvDate.setText(localTime.get(position));
// Load and display image
String imageUrl = (video.getDefaultImage128x96().get(position));
imageUrl = imageUrl.trim().replaceAll(" ", "%20");
imageLoader.displayImage(imageUrl, holder.ivIcon, options);
holder.ivIcon.buildDrawingCache();
Bitmap image = holder.ivIcon.getDrawingCache();
if(image != null) {
image = putOverlay(image, holder.ivIcon.getWidth(), holder.ivIcon.getHeight());
holder.ivIcon.setImageBitmap(image);
holder.ivIcon.destroyDrawingCache();
}
return convertView;
}
in following code, I'm giving list of URLs to that libray to be downloaded in different threads.
// Load and display image
String imageUrl = (video.getDefaultImage128x96().get(position));
imageUrl = imageUrl.trim().replaceAll(" ", "%20");
imageLoader.displayImage(imageUrl, holder.ivIcon, options);
after that in following code, i'm getting current image of imageView (which is downloaded) and ask other method to add a layer on top of this image.
holder.ivIcon.buildDrawingCache();
Bitmap image = holder.ivIcon.getDrawingCache();
if(image != null) {
image = putOverlay(image, holder.ivIcon.getWidth(), holder.ivIcon.getHeight());
holder.ivIcon.setImageBitmap(image);
holder.ivIcon.destroyDrawingCache();
}
putOverlay() method is like this:
public Bitmap putOverlay(Bitmap bmp, int width, int height) {
Bitmap overLay = BitmapFactory.decodeResource(context.getResources(), R.drawable.video_overlay);
Bitmap bmBackground = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());
Canvas canvas = new Canvas(bmBackground);
Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
canvas.drawBitmap(bmp, 0, 0, paint);
canvas.drawBitmap(overLay, width*3/8, height*3/8, paint);
return bmBackground;
}
It was functional part. What about result!
Universal Image Loader library uses predefined image as long as main image is under downloading. my code can merge this two layers.
However, when main image is downloaded it overrides previous image while i expect my overlay image sits on top of this image.
But, after scroll of screen, i can see my added image (most of images has and some of them doesn't have my added image).
any suggestion/comments would be appreciated.
Solution was easier than the thing that i was thinking.
First I removed those
holder.ivIcon.buildDrawingCache();
Bitmap image = holder.ivIcon.getDrawingCache();
if(image != null) {
image = putOverlay(image, holder.ivIcon.getWidth(), holder.ivIcon.getHeight());
holder.ivIcon.setImageBitmap(image);
holder.ivIcon.destroyDrawingCache();
}
and putOverlay() method from my list adapter and then in XML file of row (list_news_adapter), I added another imageView and set its source attribute with my image.
Wow, it works fine.

Android: showing default image in gallery's ImageView while actual image is downloaded

I believe this is pretty trivial but I can't get it to work.
I want to display a default image in gallery elements (ImageViews) while their actual image is being fetched from the net.
Right now, nothing is shown for an ImageView which its image has yet to arrive. Once it arrives it is immediately shown.
What I tried is right after the instantiation of the ImageView to call its setImageResource function like so:
final ImageView i = new ImageView(mContext);
i.setImageResource(R.drawable.loading);
But it doesn't seem to work. Below is the full getView() function.
Any help is appreciated.
Thanks.
public View getView(int position, View convertView, ViewGroup parent) {
final ImageView i = new ImageView(mContext);
i.setImageResource(R.drawable.loading);
// if the drawbale is in the buffer - fetch it from there
Drawable bufferedImage = DataManager.getInstance().getImagesBuffer()[position];
if (bufferedImage != null){
i.setImageDrawable(bufferedImage);
BitmapDrawable drawable = (BitmapDrawable) i.getDrawable();
drawable.setAntiAlias(true);
}
// if drawable is not in buffer - fetch it from the net via AsyncImageLoader
else
{
String imageUrl = DataManager.getInstance().getImageBufferInstance().getImageUrl(position);
Drawable downloadedImage = AsyncImageLoader.getInstance().loadDrawable(imageUrl, new ImageCallback() {
public void imageLoaded(Drawable imageDrawable, String imageUrl) {
if (imageDrawable == null)
{
imageDrawable = getResources().getDrawable(R.drawable.icon);
}
i.setImageDrawable(imageDrawable);
BitmapDrawable drawable = (BitmapDrawable) i.getDrawable();
drawable.setAntiAlias(true);
}
});
i.setImageDrawable(downloadedImage);
}
i.setLayoutParams(new CoverFlow.LayoutParams(Utils.getInstance().getScreenWidth() / 2,
Utils.getInstance().getScreenHeight() / 2));
i.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
return i;
}
Fix your indentation... If I'm reading that correctly, you're only setting the temporary drawable icon in the imageLoaded callback of your (not shown) AsyncImageLoader, which I assume means it's then only being set after the image downloads and is then immediately overwritten with the downloaded image. Try moving the placeholder-setting code into your else block outside the callback.

Categories

Resources