i have develop one application in which i want to play gif animation.
for that i have refer this
CODE
public class GIFDemo extends GraphicsActivity {
ImageView imageView1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView1 = (ImageView) findViewById(R.id.imageView1);
}
private static class GIFView extends View{
Movie movie,movie1;
InputStream is=null,is1=null;
long moviestart;
long moviestart1;
public GIFView(Context context) {
super(context);
is=context.getResources().openRawResource(R.drawable.border_gif);
movie=Movie.decodeStream(is);
}
#Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(0xFFCCCCCC);
super.onDraw(canvas);
long now=android.os.SystemClock.uptimeMillis();
if (moviestart == 0) { // first time
moviestart = now;
}
if(moviestart1==0)
{
moviestart1=now;
}
int relTime = (int)((now - moviestart) % movie.duration()) ;
// int relTime1=(int)((now - moviestart1)% movie1.duration());
movie.setTime(relTime);
// movie1.setTime(relTime1);
movie.draw(canvas,10,10);
//movie1.draw(canvas,10,100);
this.invalidate();
}
}
}
Main :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/>
<ImageView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/imageView1"
android:src="#drawable/icon"></ImageView>
</LinearLayout>
so problem is i want to set class GIFView in Imageview so i have refer post like this
but not get proper example. so can you give me proper example and explanation that how can i set view in ImageView
Thanks
nik
I don't think it is meaningful to 'set the view' of an ImageView (in the way you are suggesting), and I don't think you even need an ImageView in this case. You have implemented a custom View that is able to draw a GIF without any dependency on the ImageView class - you just have to use it!
With minimal changes, assuming the GIFView works, you can just instantiate a GIFView and then add it to the main layout. Let your onCreate be as follows:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// let your LinearLayout have an id of 'container'
LinearLayout container = (LinearLayout) findViewById(R.id.container);
GIFView gifView = new GifView(this);
container.addView(gifView);
}
You can get rid of the ImageView from the layout entirely and just use your GIFView that was instantiated in code. Alternatively, you can make your GIFView a standalone (non-inner) class and add it to your XML layout by specifying the full package name, e.g.
<com.nik.view.GIFView
id="#+id/gifview"
... />
You would also need to write the view constructor GIFView(Context context, AttributeSet attrs) because that is the one that is expected when using XML.
Related
in my example project :ondraw need an int to drawing from main activity, when initialize the parameter to 1 in mainActivity, its not draw anything,
//class MyView extends View
private int parameter=0;//if i change to 1 it will draw,
//but i want set it from main Activity
public void setParameter(int param)
{
this.parameter=param;
invalidate();
Log.i("tag9", this.parameter+" setParameter");
}
#Override
protected void onDraw(Canvas canvas) {
Log.i("tag9",parameter + " ondraw");
if(this.parameter==0){
return;}
//Drawing code ...
}
//in MainActivity:
MyView myView = new MyView(this);
myView.setParameter(1);
//Xml code:
<RelativeLayout ...>
<view
android:layout_width="wrap_content"
android:layout_height="wrap_content"
class="(packagename).MyView"
android:id="#+id/view"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="138dp"/>
</RelativeLayout>
//DDMS
1 setparameter
0 ondraw // its shouldnt be 0;
thnx to Gabe Sechan ,
You're creating it via new. That creates a new view not in any layout. If you want to reference a view set in xml, use findViewById – Gabe Sechan
MyView myView = (MyView)this.findViewById(R.id.view);
myView.setParameter(1);
I have a layout activity_main that (among other things) shows ImageView:
<ImageView
android:id="#+id/profileImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2"
android:layout_alignLeft="#+id/login_button" />
I've created a class that extendsImageView and shows animated gif:
public class AnimatedGif extends ImageView
{
private Movie mMovie;
private long mMovieStart = 0;
public AnimatedGif(Context context, InputStream stream)
{
super(context);
mMovie = Movie.decodeStream(stream);
}
#Override
protected void onDraw(Canvas canvas)
{
canvas.drawColor(Color.TRANSPARENT);
super.onDraw(canvas);
final long now = SystemClock.uptimeMillis();
if (mMovieStart == 0)
{
mMovieStart = now;
}
final int realTime = (int)((now - mMovieStart) % mMovie.duration());
mMovie.setTime(realTime);
mMovie.draw(canvas, 10, 10);
this.invalidate();
}
}
In the main activity I use the following code:
setContentView(R.layout.activity_main);
.
.
.
InputStream stream = this.getResources().openRawResource(R.drawable.searching_gif);
AnimatedGif gifImageView = new AnimatedGif(this, stream);
ImageView im = (ImageView)findViewById(R.id.profileImageView);
How can I make that im will show gifImageView??
you can't. At least the way you though about it. You need some minimal changes, to load your AnimatedGif directly into the layout. The first is a constructor that takes as parameter the Context and the AttributeSet:
public AnimatedGif(Context context, AttributeSet attrs) {
}
this way you can add it directly as item of the xml, specifying the full qualified package to the class
<com.package.AnimatedGif
android:id="#+id/profileImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2"
android:layout_alignLeft="#+id/login_button" />
now you probably want to have a custom attribute to specify the gif you want to load. So you could declare in your attr.xml file
<declare-styleable name="AnimatedGif">
<attr name="gifres" format="integer" />
</declare-styleable>
and inside the constructor you can load it like
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AnimatedGif);
int value = a.getInt(R.styleable.AnimatedGif_gifres, 0));
stream = context.getResources().openRawResource(value);
a.recycle();
How can I make that im will show gifImageView??
Because activity_main.xml is layout for Activity in which want to add custom ImageView using code:
1. Use LinearLayout in activity_main.xml as root Layout and set orientation to vertical
2. Assign id to root Layout of activity_main.xml like:
android:id="#+id/main_layout"
3. Get root layout in onCreate method:
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.main_layout);
4. Now add gifImageView view object to linearLayout:
linearLayout.addView(gifImageView);
I'm currently developing an Android App, using a personal SurfaceView and double buffering. However I'm facing a little problem with my code.
In one hand I have an xml view, based on LinearLayout hierarchy. When I instantiate my activity, I set my contentView on this xml. The problem is then that my double buffering don't works anymore. Thread is running but nothing is displayed.
In the other hand, I set my contentView with a new personal SurfaveView element and display works fine. But of course, I cannot access anymore to the other elements of my LinearLayout.
Actually, I would like to set my contentView on my xml view AND keep my display working.
I hope I was clear enough, thank you for your answers!
Here is my activity:
public class MainController extends Activity {
#Override
protected void onCreate(final Bundle p_savedInstanceState) {
super.onCreate(p_savedInstanceState);
setContentView(R.layout.main_controller_activity);
this.drawableView = (MCustomDrawableView) findViewById(R.id.drawingBox);
...
}
...
}
My surface view:
public class MCustomDrawableView extends SurfaceView {
public MCustomDrawableView(Context p_context, AttributeSet p_attributes) {
super(p_context, p_attributes);
this.getHolder().addCallback(new SurfaceHolder.Callback() {
#Override
public void surfaceCreated(final SurfaceHolder p_holder) {
try {
// Instantiating a new thread
thread = new MBufferingThread(getInstance());
thread.start();
} catch (Exception exception) {
exception.printStackTrace();
}
}
#Override
public void surfaceChanged(final SurfaceHolder p_holder, int p_format, int p_width, int p_height) {...}
#Override
public void surfaceDestroyed(final SurfaceHolder p_holder) {...}
});
// Setup drawing options
setupDrawing();
}
...
}
And my xml view:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:background="#color/app_background"
tools:context=".MainController">
<com.iskn.calligraphy.models.draw.MCustomDrawableView
android:id="#+id/drawingBox"
style="#style/DrawingBox" />
<SeekBar
android:id="#+id/brushSize"
style="#style/SizeSeekBar" />
<SeekBar
android:id="#+id/brushOpacity"
style="#style/OpacitySeekBar" />
</LinearLayout>
EDIT:
After further researches and analyses, it appears clearly that:
setContentView(R.layout.main_controller_activity): in this case I get all the elements from my activity, but the MCustomDrawableView display nothing.
setContentView(new MCustomDrawableView(getApplicationContext())): on that case, MCustomDrawableView is working well (it displays what I want), but I don't have the others View from my main_controller_activity
In both cases:
my thread is running and works well.
my drawing function is called as well, with the holder.lockCanvas() and holder.unlockCanvasAndPost(bufferCanvas) methods.
Well, I found a functionnal solution :
I think the problem was coming from the MCustomDrawableView initialization . I created a new instance of that class from the onCreate method , and not from the xml file . Then, I set it to the contentView:
protected void onCreate(Bundle p_savedInstanceState) {
super.onCreate(p_savedInstanceState);
setContentView(R.layout.main_controller_activity);
// Instantiate drawable object & set style properties
this.drawableView = new MCustomDrawableView(getApplicationContext());
FrameLayout.LayoutParams style = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT);
// Add the drawable view to the main_activity
addContentView(this.drawableView, style);
}
But this way raises a new problem. The added view is on the top, which means that all the others View are hided. I solved this with bringToBack(this.drawableView) below method:
private void bringToBack(View p_view) {
// Get parent from the current view
ViewGroup viewGroup = ((ViewGroup) p_view.getParent());
int childrenCount = viewGroup.indexOfChild(p_view);
for(int cpt = 0; cpt < childrenCount; cpt++) {
// Move the child to the top
viewGroup.bringChildToFront(viewGroup.getChildAt(cpt));
}
}
It brings back the provided view to the background position. I also had to set the LinearLayout's alpha to 0.
I'm still open to other solutions!
I have a LinearLayout within a ScrollView in the xml file. I need to paint and write within the ScrollView so I've created a view in the layout and have been able to paint and write inside using: canvas.draw() and canvas.drawText(), my problem comes when writing text with canvas are not clearly the letters, so I need to add a TextView to the layout of the ScrollView from the class, without knowing very well how to do it.
Paste the code:
public class Calendario extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calendar);
LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
MyView v = new MyView(this);
layout.addView(v,250,2000);
}
private class MyView extends View{
public MyView(Context context) {
super(context);
}
protected void onDraw(Canvas canvas) {
onDrawMethod();
//Here draw and write text//
}
}
}
A picture of what I need: I need only add textviews inside
Thankss
As I understand you, you want to add those texts programmaticaly, by saying
so I need to add a TextView to the layout of the ScrollView from the class
If so, might be this post will help you.
I am trying to make a thermometer. For this I have an image of a thermometer(a png file) which i have inserted
into the layout using ImageView. Now I want to draw a line over this image to show the effect of temperature reading.
Here is my code.
main.xml
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
</ImageView>
<com.focusone.Android.DrawExample.CustomDrawableView
android:id="#+id/custom_drawable_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
height='100'
/>
</AbsoluteLayout>
DrawExample.java
package com.focusone.Android.DrawExample;
public class DrawExample extends Activity {
/** Called when the activity is first created. */
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
CustomDrawableView.java
public class CustomDrawableView extends View {
private ShapeDrawable mDrawable;
public CustomDrawableView(Context v,AttributeSet as){
super(v,as);
drawShape(as);
}
public void drawShape(AttributeSet ast) {
int x =45; int y=15 ; int width=5; int height=0 ;
height= Integer.parseInt(ast.getAttributeValue(null, "height"));
mDrawable = new ShapeDrawable(new RectShape());
mDrawable.getPaint().setColor(0xff74AC23);
mDrawable.setBounds(x, y, x + width, y + height);
}
protected void onDraw(Canvas canvas) {
mDrawable.draw(canvas);
}
}
Now I can draw a line on top of the thermometer image using this program.
But how do i change the height of line during runtime.
Since height=100 is written in the xml file I don't know how I can change it.
Please somebody help me.
First I think you don't need to pass attributes in drawShape(AttributeSet ast) but just height in your constructor CustomDrawableView(). Then as Chris said you should have another method in the custom view like
public void setTemperature(int tmp){
drawShape(tmp);
invalidate(); //this will call onDraw()
}
In your main activity make a reference to your custom view so you can call its public methods
CustomDrawableView temperatureView =
(CustomDrawableView) findViewById(R.id.custom_drawable_view);
temperatureView.setTemperature(50);
You can:
Make "height" a member of your CustomDrawableView
Then create a method setHeight(int height) where you change the value of height.
Then in your activity use (CustomDrawableView)findViewById(R.id.custom_drawable_view) to get a reference to your viewObject.
Then change the value as you like with calling setHeight(x)
hope that helps