In my app i got three custom views in single activity. So now i need to save it as a single jpeg. How to acheive this. I know how to save one or two bitmaps but this time its three custom views in single activity.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<com.wglxy.example.pinchzoompan.PanZoomView
android:id="#+id/zoomview1"
android:layout_width="100dp"
android:layout_height="700dp"
/>
<com.wglxy.example.pinchzoompan.PanZoomView1
android:id="#+id/zoomview2"
android:layout_width="100dp"
android:layout_height="700dp"
/>
<com.wglxy.example.pinchzoompan.PanZoomView2
android:id="#+id/zoomview3"
android:layout_width="100dp"
android:layout_height="700dp"
/>
</LinearLayout>
</FrameLayout>
each view has a picture, now i need to save it as a single jpeg. Can anyone suggest me some ideas.
Inflate your xml into single view by using layoutinflater then create a bitmap of that view.
i got the my three views saved as a single jpeg file with this code
layout = (LinearLayout) findViewById(R.id.viewLayout);
layout.setDrawingCacheEnabled(true);
layout.buildDrawingCache();
viewbitmap = layout.getDrawingCache(true);
and then
try {
viewbitmap.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(f));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
just place all child in parent layout then setDrwaingCache true for that parent view after that whenever you need to take or save all child on that parent view you can getDrawingCache for that parent.
Related
My question is if is possible add an ImageView in a SurfaceView without XML. If yes, how? I have a main class that has the function of GamePanel, and for apply a Method i need to call it with an ImageView, but i don't know if it is possible. Thanks you in advance.
You need to read about the View and ViewGroups provided by the Android Framework.
I am giving the quick understanding to propose the solution.
Crash Course about View & ViewGroup
At the root of the Android UI system, everything is View.
What is a View?
It is a single widget / UI component that can be displayed on the screen. The View includes Buttons, TextViews, ImageViews, SurfaceView. They can not contain any child view i.e. They can not hold declaration for the any other child view
Following XML definition is incorrect: A view can not hold another view
<SurfaceView
android:id="#+id/textSurfaceView"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</SurfaceView>
What is ViewGroup?
Inherited from View and designed to contain and arrange more than one View also called as Child views. The various ViewGroups are LinearLayout, RelativeLayout, FrameLayout etc.
Following XML definition is Correct: A ViewGroup can hold another view
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<SurfaceView
android:id="#+id/surfaceView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</FrameLayout>
Here comes the solution
Step-1: Add a ViewGroup in your XML wrapping the existing SurfaceView. As mentioned already the ViewGroups are LinearLayout, RelativeLayout, FrameLayout etc.
res/layouts/your_layout.xml
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/baseFrame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<SurfaceView
android:id="#+id/surfaceView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</FrameLayout>
Step-2: At the time of view creation add an ImageView to the FrameLayout. onCreate() activity.
setContentView(R.layout.your_layout);
FrameLayout baseFrame = (FrameLayout) findViewById(R.id.baseFrame);
ImageView imageView = new ImageView(this);
imageView.setWidth(/*As per your need*/);
imageView.setHeight(/*As per your need*/);
imageView.setId(/*Any unique positive Number*/ R.ids.imageView1); <= Required to access this view later
/*Set the layout parameters such as layout_gravity as well.*/
baseFrame.addView(imageView);
Step-3: I know you must be wondering about the ImageView Id. I am giving the quicker way to assign an ID to a View.
Create a file ids.xml at res/values
Fill the following details.
<resources>
<item type="id" name="imageView1" />
</resources>
Step-4: Passing an ImageView to the method
ImageView myImageView = (ImageView) findViewById(R.id.imageView1);
methodToBeCalled(myImageView);
I hope that helps.
Happy Coding!!!
I wrote a small function
private void addTabIndicators(int tabCount){
LinearLayout indicatorsContainer = (LinearLayout)findViewById(R.id.indicators_container);
for(int i = 0; i<tabCount; i++){
ImageView indicator = (ImageView)this.getLayoutInflater().inflate(R.layout.tab_indicator, null);
indicatorsContainer.addView(indicator);
}
}
that is supposed to add circles to the linearlayout in my activity based on how many tabs are in the pager adapter. Everything would be cool BUT, the imageviews i add instead of beeing the size declared in the xml layout, are being resized to 1x1px... Any ideas where i could go wrong? Here are the layouts for the indicator and linear layout
tab_indicator.xml:
<?xml version="1.0" encoding="utf-8"?>
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_margin="8dp"
android:src="#drawable/floating_button_background"/>
The indicators container:
<LinearLayout
android:layout_marginBottom="16dp"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:id="#+id/indicators_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</LinearLayout>
The fault might be here
ImageView indicator = (ImageView)this.getLayoutInflater().inflate(R.layout.tab_indicator, null);
You need to pass the root view of your image view to provide the layouts defined in your XML. If you pass null instead, default layoutparams are set.
Place
ImageView indicator = (ImageView)this.getLayoutInflater().inflate(R.layout.tab_indicator, your root view of image view,false);
This is a very common mistake. Never pass null except you really know what you are doing.
Read more here:
http://developer.android.com/reference/android/view/LayoutInflater.html
Hope it helps
I am desperately trying to achieve this for a quite long time but still could not make it work.
I have a ListView where in inside my Adapter I am trying to add image views dynamically based on my data. But, ScrollView does not show up with added images. When I tried myLinearLayout.getChildCount(), it gives proper results!!
What am I doing wrong? Any help is much appreciated :)
<!-- ScrollView (This lies inside listView's child view) -->
<HorizontalScrollView
android:id="#+id/card_txn_horizontal_scroll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="left">
<LinearLayout
android:id="#+id/scroll_view_linear"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:background="#color/header_bg">
</LinearLayout>
</HorizontalScrollView>
LinearLayout sharedWithView = (LinearLayout) convertView.findViewById(R.id.scroll_view_linear);
for (User sharer : data.getSharerModelList()) {
ImageView mImageView = (ImageView) mInflator.inflate(R.layout.photo, null);
Picasso.with(mImageView.getContext())
.load(expense.getPayeeModel().getProfilePicUrl())
.centerCrop()
.resize(QuickReturnUtils.dp2px(mImageView.getContext(), 38),
QuickReturnUtils.dp2px(mImageView.getContext(), 38))
.placeholder(R.drawable.ic_launcher)
.error(android.R.drawable.stat_notify_error)
.into(mImageView);
sharedWithView.addView(mImageView);
}
try android:layout_width="wrap_content" inside the LinearLayout
I wonder what's the best way to create a photo gallery for my app. I get the pictures from an RSS feed and I want to display each of them in fullscreen. What should I use ? I tried with an ImageView but the images are resized in a bad way (they don't keep the original dimensions)
I used this answer to implement my gallery : Android Gallery fullscreen
Thank's
EDIT :
Here's the XML layout :
<?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" >
<android.support.v4.view.ViewPager
android:id="#+id/galleryPager"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>
</LinearLayout>
And here's the code I use in my adapter to load the images :
#Override
public Object instantiateItem(View collection, int position) {
ImageView imageView = new ImageView(context);
imageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
ImageDownloader.getInstance().download(gallery.get(position), ImageFormat.CONTENT_316, imageView);
((ViewPager) collection).addView(imageView, 0);
return imageView;
}
I would use a ViewPager with ImageViews inside of it.
You are able to specify to the ImageView how you'd like the images to be (or not to be) streched with the android:scaleType attribute in your xml. If you share some of the code that you were using with ImageViews and maybe give a little bit more info about how you wan them to appear I can try to help you get it set up properly.
Also I suggest you take a look at Displaying Bitmaps Efficiently for best practices to use when dealing with images so as to keep the performance of your app top notch.
Use an Adapter & ListView to display images slide.
Now in the adapter item's layout use ImageView with fill_parent for both width & hight as like following code:
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY" />
you may suggest the user to use the phone in landscape mode for better performance. so keep additional layout in layout-land folder
one way would be
get the image url's from the rss feed
show them as thumbnails in a custom gallery (can setup via horizontal linearlayout with a imageview in it, inside a scrollview)
use lazyloading of images to show these thumbnails
override onClickListener to get the click events
and in this onClick load the current image URL in a webview.
webview has good features of zoom, move along picture etc..
The code I used above was almost right, I just changed ImageView.ScaleType.FIT_XY to ImageView.ScaleType.CENTER_CROP
<?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" >
<android.support.v4.view.ViewPager
android:id="#+id/galleryPager"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v4.view.ViewPager>
</LinearLayout>
And here's the code I use in my adapter to load the images :
#Override
public Object instantiateItem(View collection, int position) {
ImageView imageView = new ImageView(context);
imageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
imageView.setScaleType(**ImageView.ScaleType.CENTER_CROP**);
ImageDownloader.getInstance().download(gallery.get(position), ImageFormat.CONTENT_316, imageView);
((ViewPager) collection).addView(imageView, 0);
return imageView;
}
I am attempting to write code that would allow me to have 1-2 views at the top of a listview that are of a different arrangement than the other views in the listview. I decided to try the method of using addHeaderView() for the 1-2 views that will differ in display.
I have two xml layout files, one that defines the view format that most of the listview views will fall under (list_image_checkbox_row.xml) and one for the Header Views (catalog_featured_row.xml). The adapter constructor uses list_image_checkbox_row as its resource in the constructor.
I used a Thread to set the adapter and load the views. I am able to programmatically create views and use addHeaderView to at them to the listview (using the same image resource), but I get errors when I try to use addHeaderView on a view I have inflated from a layout xml file.
handler.post(new Runnable() {
#Override
public void run() {
lv = (ListView)findViewById(R.id.sources_list);
Activity context = BrowseSourceActivity.this;
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.catalog_featured_row, null);
ImageView feat_view = (ImageView) view.findViewById(R.id.image);
feat_view.setImageResource(R.drawable.arrow);
lv.addHeaderView(feat_view);
setListAdapter(array);
}
});
I get a force close error and haven't been able to find anything in logcat (when I was trying to debug yesterday I got a NullPointerException). If I comment out the lv.addHeaderView(feat_view) line, the application does not force close on me. Here is the code for featured_catalog_row.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="70dip"
android:padding="5dip">
<ProgressBar android:id="#+id/spinner"
android:indeterminate="true"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_centerInParent="true"
/>
<ImageView android:id="#+id/image"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
Any suggestions for how to get the inflater to work?
Perhaps you should store lv in an instance variable instead of looking it up dynamically. Note that both View and Activity have a method called findViewById, and so your results will depend on how your code is organized.