I don't have any XML-Layout's. I directly start my Activity like that
super.onCreate(savedInstanceState);
setContentView(new DrawingPanel(this));
It's a drawing App and the default Background is Black. I want to change the Background to an ImageBackground. How can I do that?
EDIT: IT'S NOT ABOUT JUST SETTING A BACKGROUND. I´ve got a SurfaceView (DrawingPanel) with a black Background by default. I want to change the black background to a Picture, so I can draw ON the picture.
// try this way,hope this will help you...
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout parent = new LinearLayout(this);
parent.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
parent.setBackgroundResource(R.drawable.ic_launcher); // here set your background
parent.addView(new DrawingPanel(this));
setContentView(parent);
}
I support #haresh, I think the problem occur because you are trying to load big images. Here is a good tutorial how to load large bitmaps.
http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
Bitmap background = BitmapFactory.decodeResource(getResources(), R.drawable.mypicture);
scaled = Bitmap.createScaledBitmap(background, width, height, true);
and then using
canvas.drawBitmap(scaled, 0, 0, null);
solved that problem!
Below code is working fine to set the background image.
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.drawable.ic_launcher);
setContentView(imageView);
}
this works fine for me
logo = new ImageView(this);
logo.setImageResource(R.drawable.sync_cloud_logo);
logo.setBackgroundColor(getResources().getColor(R.color.drawer_bkground));// your color
setContentView(logo);
Related
This question already has an answer here:
Combining 2 Images overlayed
(1 answer)
Closed 5 years ago.
I need to merge two images, one on top of another. I have the first image (background.png) where it has an transparent portion. I want to put another image (image.png) on top of the background.png.
However for the final image that gets created, I only want part of the image.png that overlaps with the transparent portion of the bakground.png to be shown, the rest of the new image will show the background.
Here's my code for merging the image, but I'm not sure how to go from here. Thanks.
public class MainActivity extends ActionBarActivity {
private ImageView collageImage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
collageImage = (ImageView)findViewById(R.id.imageView3);
Button combineImage = (Button)findViewById(R.id.combineimage);
combineImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Bitmap bigImage = BitmapFactory.decodeResource(getResources(), R.drawable.multiple);
Bitmap smallImage = BitmapFactory.decodeResource(getResources(), R.drawable.multipletwo);
Bitmap mergedImages = createSingleImageFromMultipleImages(bigImage, smallImage);
collageImage.setImageBitmap(mergedImages);
}
});
}
private Bitmap createSingleImageFromMultipleImages(Bitmap firstImage, Bitmap secondImage){
Bitmap result = Bitmap.createBitmap(firstImage.getWidth(), firstImage.getHeight(), firstImage.getConfig());
Canvas canvas = new Canvas(result);
canvas.drawBitmap(firstImage, 0f, 0f, null);
canvas.drawBitmap(secondImage, 10, 10, null);
return result;
}
}
Try using 2 imageviews that overlap in the layout. Maybe use a Relative layout to position them how you want. Then you will set an image for each iamgeview and they will be "merged".
I have a simple activity which display a fullscreen SVG thanks to this
android lib. The SVG displayed is a map and I need to enable zoom/scroll gesture on it. That is why I use this lib, which works perfectly with normal ImageView.
Here, I'm using PhotoView lib attacher to SVGImageView :
PhotoViewAttacher mAttacher;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout layout = new LinearLayout(this);
SVGImageView svgImageView = new SVGImageView(this);
svgImageView.setImageAsset("map.svg");
layout.addView(svgImageView, new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
setContentView(layout);
mAttacher = new PhotoViewAttacher(svgImageView);
}
PhotoView works but it takes like 10 second to apply zoom or scroll gesture.
Do you have any ideas on how I could efficiently scale & scroll in ?
Thank you in advance.
In my Activity I need to change an ImageView background using a gradient, so I use an image with a transparent area, changing its background when I need. Here's some code:
private static View myImage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myActivityLayout);
myImage = findViewById(R.id.myImageID);
}
[...]
private void myImageUpdate() {
GradientDrawable gradient;
int[] colors = {0xFF00FF00, 0xFF0000FF};
// I make some changes to these colors..
gradient = new GradientDrawable(GradientDrawable.Orientation.BOTTOM_TOP, colors);
myImage.setBackgroundDrawable(gradient);
}
Now, the problem is:
If I call myImageUpdate() within onCreate() method, everything works fine.
If I call myImageUpdate() from another part of the code (like an onClick callback), I can't set my backgroud!
* UPDATE *
Guys, this code is fine... I was calling my method in a wrong (not directly reachable) line! My apologies...
I don't think this will fix it since you said myImageUpdate gets called within onClick... but try this..
runOnUiThread(new Runnable() {
public void run() {
myImage.setBackgroundDrawable(gradient);
}
});
you might have to make gradient variable final..
final GradientDrawable gradient = new GradientDrawable(GradientDrawable.Orientation.BOTTOM_TOP, colors);
Try myImage.invalidate(). This will force the system to redraw the view.
I am facing a problem in my android app. I have to dynamically find the height of the text description coming from parsing the xml and show it on an imageview(background image).
I have to repeat the backround image by finding the total height of the text.
So, Please help me that how can we find the text height dynamically and repeat the background image according to that height.
Thanks.
Since there is no answer for this question.
I tried and almost did it in the following way:
public class MainActivity extends Activity
{
String description="hello how r u this project is meant for android to calculate " +
"the size of text and can dyanamically set the image height according to the text";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
int length_count=description.length();
int window_width=getWindowManager().getDefaultDisplay().getWidth();
int lines=(length_count*14)/(window_width-30);
Log.v("length",""+length_count);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.project_dess_box_mid);
BitmapDrawable bitmapDrawable = new BitmapDrawable(bmp);
bitmapDrawable.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
LinearLayout layout = (LinearLayout)findViewById(R.id.linear1);
TextView tv=(TextView)findViewById(R.id.text01);
tv.setLines(lines);
tv.setText(description);
layout.setBackgroundDrawable(bitmapDrawable);
}
}
I posted this as it may be helpful for others too.
I'm trying to draw an image inside a do while in the OnCreate method but it fails to show.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
drawElements();
}
public void drawElements(){
do{
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(80, 60);
params.setMargins(0, 0, 0, 0);
ImageView image = new ImageView(this);
personaje.setImageResource(R.drawable.image);
layout.addView(image, params);
setContentView(layout);
Log.i("MainActivity","here comes");
}while(!esc)
}
It's strange because it comes in the do while, because the log shows constantly on the LogCat but the image does not show.
In fact, it fails to remove the status bar or the title of the application as specified in the onCreate, but if I remove the do while it shows everything correctly.
Any suggestions?
You need to rethink your whole design. What you are doing here does not make any sense.
Your while loop is going to loop forever, blocking the UI thread. Thus, nothing will ever be displayed.
Instead, you should display a single image in onCreate. Add a key listener or button listener that will change the image within the listener.