i have a custom view to show a gif animated image. The problem born when i put in my linearlayout the custom view, the other elements under my custom view aren't displayed. this is my view:
public class GIFView extends View{
private Movie movie;
private InputStream is;
private long moviestart;
private long duration;
public GIFView(Context context) {
super(context);
is=getResources().openRawResource(R.drawable.anim_cerca);
movie=Movie.decodeStream(is);
duration = movie.duration();
}
public GIFView(Context context,AttributeSet set){
super(context,set);
is=getResources().openRawResource(R.drawable.anim_cerca);
movie=Movie.decodeStream(is);
duration = movie.duration();
}
public GIFView(Context context,AttributeSet set,int def){
super(context,set,def);
is=getResources().openRawResource(R.drawable.anim_cerca);
movie=Movie.decodeStream(is);
duration = movie.duration();
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
long now=android.os.SystemClock.uptimeMillis();
Paint p = new Paint();
p.setAntiAlias(true);
if (moviestart == 0)
moviestart = now;
int relTime = (int)((now - moviestart) % duration);
movie.setTime(relTime);
movie.draw(canvas,0,0);
this.invalidate();
}
}
and this is my layout xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent" android:background="#drawable/sfondo">
<LinearLayout android:id="#+id/linearLayout2"
android:layout_height="wrap_content" android:layout_width="match_parent"
android:orientation="vertical">
<LinearLayout android:id="#+id/linearLayout1"
android:layout_height="fill_parent" android:weightSum="1"
android:layout_width="fill_parent" android:orientation="horizontal">
<ImageView android:layout_height="73dp" android:id="#+id/imageView1"
android:layout_weight="0.25" android:src="#drawable/trovachiavi"
android:layout_width="256dip"></ImageView>
<ImageButton android:id="#+id/infoButton"
android:background="#null" android:layout_height="47dp"
android:layout_marginLeft="10dip" android:layout_weight="0.25"
android:layout_marginTop="15dip" android:src="#drawable/info_mini"
android:layout_width="47dp"></ImageButton>
</LinearLayout>
</LinearLayout>
<LinearLayout android:id="#+id/layoutGIF"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:orientation="vertical"
android:layout_gravity="center">
</LinearLayout>
<ImageButton android:id="#+id/avvia_cerca"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_marginTop="10dp" android:layout_gravity="center|bottom"
android:background="#null"></ImageButton>
</LinearLayout>
in the activity i add the custom view to layout with id layoutGIF in this way:
LinearLayout lg = (LinearLayout)findViewById(R.id.layoutGIF);
lg.setGravity(Gravity.CENTER);
gif = new GIFView(this);
lg.addView(gif, new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT));
what can i do? i need the elements under this!
best way is using animation.. don't think that gif is solution.. is not supported from every android device!
Related
My activity is in LANDSCAPE mode
I am using following code.
Activity_main.xml
<RelativeLayout 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">
<TextView
android:id="#+id/sample"
android:layout_height="80dp"
android:layout_width="match_parent"
android:text="saga"/>
<com.example.sample.Circle
android:id="#+id/circle"
android:background="#AD9999"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/sample"/>
</RelativeLayout>
Circle.java
public class Circle extends View{
public Circle(Context context) {
super(context);
this.context=context;
}
public Circle(Context context, AttributeSet attrs) {
super(context, attrs);
this.context=context;
mOuterDialPaint.setColor(Color.BLUE);
}
#Override
public void onDraw(Canvas canvas)
{
float width=canvas.getWidth()/2;
Log.i("Width", ""+canvas.getHeight());
float dialCenterX=width;
canvas.drawCircle(dialCenterX, canvas.getHeight(), dialCenterX, mOuterDialPaint);
}
}
So canvas is drawing circle from specified positions but not moving below textview so textview is coming on top of canvas and some part is getting cut. So any fix for this problem??
Use frame-Layout in xml:
<FrameLayout 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" >
<com.example.sample.Circle
android:id="#+id/circle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#AD9999" />
<TextView
android:id="#+id/sample"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="top"
android:gravity="center"
android:text="your text view" />
</FrameLayout>
I have list of button in bottom of the page which is in horizontal listview, i want to show only 4 row in listview, when screen become bigger it's item size should also become bigger so only 4 item shows.
for example
here you can see that after increasing size of width it's related horizontal listview's listitem also increasing
is it possible? if possible than can any body suggest me how we can handle it ?
Create one custom layout class like below..
public class MyLinearLayout extends LinearLayout {
public MyLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyLinearLayout(Context context) {
super(context);
}
public void setLayoutParams(int width) {
LayoutParams params = new LayoutParams(width / 4,
LayoutParams.WRAP_CONTENT);
this.setLayoutParams(params);
}
#Override
public void setLayoutParams(android.view.ViewGroup.LayoutParams params) {
super.setLayoutParams(params);
}
}
put this horizontal scrollview in your xml file
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<com.example.widget.MyLinearLayout
android:id="#+id/llMainHome"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/orange_bg"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/home" />
</com.example.widget.MyLinearLayout>
<com.example.widget.MyLinearLayout
android:id="#+id/llHomeSearch"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/green_bg"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/search" />
</com.example.widget.MyLinearLayout>
<com.example.widget.MyLinearLayout
android:id="#+id/llHomeFavorite"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/green_bg"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/favorite" />
</com.example.widget.MyLinearLayout>
<com.example.widget.MyLinearLayout
android:id="#+id/llHomeGrocery"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/green_bg"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/make_juice" />
</com.example.widget.MyLinearLayout>
<com.example.widget.MyLinearLayout
android:id="#+id/llHomeSettings"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/green_bg"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/settings" />
</com.example.widget.MyLinearLayout>
</LinearLayout>
</HorizontalScrollView>
And in your activity class file declare
private MyLinearLayout llHomeSearch, llHomeFavorite,
llHomeGrocery, llHomeSettings,llMainHome;
oncreate Method code is
llHomeFavorite = (MyLinearLayout) findViewById(R.id.llHomeFavorite);
llHomeFavorite.setOnClickListener(this);
llMainHome = (MyLinearLayout) findViewById(R.id.llMainHome);
llMainHome.setOnClickListener(this);
llHomeSearch = (MyLinearLayout) findViewById(R.id.llHomeSearch);
llHomeSearch.setOnClickListener(this);
llHomeGrocery = (MyLinearLayout) findViewById(R.id.llHomeGrocery);
llHomeGrocery.setOnClickListener(this);
llHomeSettings = (MyLinearLayout) findViewById(R.id.llHomeSettings);
llHomeSettings.setOnClickListener(this);
Display display = getWindowManager().getDefaultDisplay();
Point size = getDisplaySize(display);
int width = size.x;
llMainHome.setLayoutParams(width);
llHomeFavorite.setLayoutParams(width);
llHomeGrocery.setLayoutParams(width);
llHomeSearch.setLayoutParams(width);
llHomeSettings.setLayoutParams(width);
and getDisplaysize method is
#SuppressLint("NewApi")
private static Point getDisplaySize(final Display display) {
final Point point = new Point();
try {
display.getSize(point);
} catch (java.lang.NoSuchMethodError ignore) { // Older device
point.x = display.getWidth();
point.y = display.getHeight();
}
return point;
}
here you go..it will work nice..
there is no need to take horizontal list view if you have fix number of item in your tab bar..
Maybe try to get Screen width and then set List's element to 1/4 of that width?
Something like in onResume/onCreate
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
width = size.x; //remember that width
And in getView
LayoutParams params=new LayoutParams(width/4, -1);
element.setLayoutParams(params)
My XML:
<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:orientation="horizontal"
android:weightSum="1" >
<LinearLayout
android:id="#+id/leftLayout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".5"
android:orientation="vertical"
android:weightSum="1" >
<LinearLayout
android:id="#+id/firstProblem"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".5"
android:weightSum="1" >
<com.student.spelling.SpellViewFirst
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="#android:color/black" />
<LinearLayout
android:id="#+id/secondProblem"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".5"
android:weightSum="1" >
<com.student.spelling.SpellViewFirst
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>
<View
android:layout_width="3dp"
android:layout_height="fill_parent"
android:background="#android:color/black" />
<LinearLayout
android:id="#+id/rightLayout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".5"
android:orientation="vertical"
android:weightSum="1" >
<LinearLayout
android:id="#+id/thirdProblem"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".5"
android:weightSum="1" >
<com.student.spelling.SpellViewFirst
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="#android:color/black" />
<LinearLayout
android:id="#+id/fourthProblem"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".5"
android:weightSum="1" >
<com.student.spelling.SpellViewFirst
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
My View Class :
public class SpellViewFirst extends View {
private StartActivity studentActivity;
private int screenWidth;
private int screenHeight;
private Bitmap imageBitmap;
private ArrayList<Character> charList;
List<SpellObjects> spellObjectsList;
private String word;
int placeHolderHeight;
private ArrayList<Integer> dragable_id_object_list;
private Bitmap placeHolderBitmap;
Point placeHolderPoints;
Point alphabetPoints;
private int indexOfCurrentObject;
private int offsetX;
private int offsetY;
public SpellViewFirst(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
studentActivity = (StartActivity)context;
}
#Override
protected void onSizeChanged(int width, int height, int oldwidth, int oldheight) {
// TODO Auto-generated method stub
super.onSizeChanged(width, height, oldwidth, oldheight);
screenWidth = width;
screenHeight = height;
assignCoordinatesToImages();
}
private void assignCoordinatesToImages() {
// TODO Auto-generated method stub
Bitmap bm=BitmapFactory.decodeFile("/mnt/sdcard/resources/images/spelling/ques.png");
dragable_id_object_list=new ArrayList<Integer>() ;
spellObjectsList=new ArrayList<SpellObjects>();
SpellObjects curentObjects;
placeHolderHeight=bm.getHeight();
word=studentActivity.correctWordsArray[0];
imageBitmap=BitmapFactory.decodeFile("/mnt/sdcard/resources/images/spelling/"+word+".png");
placeHolderBitmap=Bitmap.createScaledBitmap(bm, screenWidth/word.length(), screenWidth/word.length(),true);
charList=new ArrayList<Character>();
placeHolderPoints=new Point();
for (int i = 0; i < word.length(); i++) {
charList.add(word.charAt(i));
}
for (int i = 0; i < charList.size(); i++) {
placeHolderPoints.y=screenHeight/2;
curentObjects=new SpellObjects(placeHolderPoints, placeHolderBitmap, -1,charList.get(i), null, placeHolderBitmap.getHeight(), screenWidth/word.length(), true);
placeHolderPoints.x=placeHolderPoints.x+screenWidth/word.length();
spellObjectsList.add(curentObjects);
}
Collections.shuffle(charList);
alphabetPoints=new Point();
alphabetPoints.x=imageBitmap.getWidth();
for (int i = 0; i < charList.size(); i++) {
Bitmap bitmap= BitmapFactory.decodeFile("/mnt/sdcard/resources/images/spelling/"+charList.get(i)+".png");
curentObjects=new SpellObjects(alphabetPoints,bitmap, i,charList.get(i), null, placeHolderBitmap.getHeight(), screenWidth/word.length(), true);
alphabetPoints.x=alphabetPoints.x+bitmap.getWidth();
spellObjectsList.add(curentObjects);
dragable_id_object_list.add(i, i);
}
}
#Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawBitmap(imageBitmap, 0, 0, null);
for(SpellObjects currentObjects :spellObjectsList ){
if(currentObjects.getIsCorrectlyPlaced())
canvas.drawBitmap(currentObjects.getobjectBitmap(), currentObjects.getCurrentPoint().x,currentObjects.getCurrentPoint().y, null);
}
}
}
I want to render like cake,duck and good in other three quadrants.Actually this is spelling app for kids where they can drag and drop alphabets to respective positions.Please help me out I am stuck.
I want to render like cake,duck and good in other three quadrants.
To set different images for your custom View you'll need to add a custom attribute for your custom layout to signal the desired image(or an id, or something to uniquely identify the view). Something like this(in attr.xml):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CustomAttr">
<attr name="pictureName" format="string" />
</declare-styleable>
</resources>
This attribute will be used in the layout like this:
<com.student.spelling.SpellViewFirst
android:layout_width="match_parent"
android:layout_height="match_parent"
newtag:pictureName="one_of_your_picture_names_here"/>
Don't forget to set the newtag namespace in the root of your xml layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:newtag="http://schemas.android.com/apk/res/your.package.here"
// ...
Then you'll use that attribute to make the SpellViewFirst use the targeted image:
public SpellViewFirst(Context context, AttributeSet attrs) {
super(context, attrs);
studentActivity = (StartActivity)context;
if (attrs != null) {
TypedArray ta = context.obtainStyledAttributes(attrs,
R.styleable.CustomAttr, 0, 0);
word = ta.getString();// this is what you use to get the picture name, right?
ta.recycle();
}
}
Then you could use the word variable to get the desired Bitmap in assignCoordinatesToImages(). Don't forget to test word for null(meaning a picture name wasn't set in the layout) and show a default image instead.
I am coding an Android game in which 2 players play at the same time. One player faces the phone in a normal way and the other faces it upside down.
As it is a quiz, I didn't use any canvas or graphics. It contains only 2 linear layouts and one of them is supposed to be upside down. For this I have used:
android:rotation="180"
for one of the layouts.
it showed upside down on the graphical view of my xml in Eclipse but when I run it on an emulator or a phone it is not inverted or rotated to 180 degrees.
XML
<?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" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" android:clipToPadding="false"
android:rotation="180">
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
</LinearLayout
Java Code
public class x extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
How can I rotate it without complication. I am presently using api level 15 but i have no restriction on API level.
I would suggest that you create a custom Layout that will handle the rotation on it's onDraw()
public class RotatedLinearLayout extends RotatedLinearLayout {
final boolean topDown;
public RotatedLinearLayout (Context context){
super(context);
}
public RotatedLinearLayout (Context context, AttributeSet attrs){
super(context, attrs);
}
public RotatedLinearLayout (Context context, AttributeSet attrs, int defStyle){
super(context, attrs,defStyle);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
super.onMeasure(heightMeasureSpec, widthMeasureSpec);
setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
}
#Override
protected void onDraw(Canvas canvas){
TextPaint textPaint = getPaint();
textPaint.setColor(getCurrentTextColor());
textPaint.drawableState = getDrawableState();
canvas.save();
if(topDown){
canvas.translate(getWidth(), 0);
canvas.rotate(180);
}else {
canvas.translate(0, getHeight());
canvas.rotate(-180);
}
canvas.translate(getCompoundPaddingLeft(), getExtendedPaddingTop());
getLayout().draw(canvas);
canvas.restore();
}
}
When you write your XML, use the VerticalRelativeLayout instead of the other layout you tried to create.
<?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" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<path.to.package.RotatedLinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" android:clipToPadding="false"
>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</path.to.package.RotatedLinearLayout >
</LinearLayout
I just created a view for displaying PDF file
public class PDFGraphic extends View{
String mText;
float mLastX;
float mLastY;
public float mOffX;
public float mOffY;
Canvas mCan;
Bitmap mBi;
public PDFGraphic(Context context, AttributeSet attrs){
super(context,attrs);
setPageBitmap();
setBackgroundColor(Color.TRANSPARENT);
}
public void uiInvalidate() {
postInvalidate();
}
public void setPageBitmap() {
mBi = Bitmap.createBitmap(100, 100, Config.RGB_565);
mCan = new Canvas(mBi);
mCan.drawColor(Color.RED);
}
public void onDraw(Canvas canvas) {
Paint paint = new Paint();
canvas.drawBitmap(mBi, 0, 0, paint);
}
}
And this is my xml file(pdfview):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" android:orientation="vertical">
<RelativeLayout android:id="#+id/relativeLayout1" android:layout_width="match_parent" android:gravity="bottom|center" android:layout_height="wrap_content" android:minHeight="30px" android:visibility="visible">
<com.shawnprojectPDF.PDFGraphic android:id="#+id/pdfview1" android:layout_width="match_parent" android:layout_above="#+id/editText1" android:layout_alignParentTop="true" android:layout_height="match_parent"></com.shawnprojectPDF.PDFGraphic>
<ImageButton android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="#drawable/leftarray" android:id="#+id/left" android:layout_alignParentBottom="true" android:layout_marginLeft="68px"></ImageButton>
<EditText android:id="#+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="#+id/left" android:layout_alignParentBottom="true" android:gravity="center" android:width="100px" android:singleLine="true" android:maxLength="12"></EditText>
<ImageButton android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="#+id/right" android:src="#drawable/rightarray" android:layout_toRightOf="#+id/editText1" android:layout_alignParentBottom="true"></ImageButton>
</RelativeLayout>
</LinearLayout>
In my main activity,if setContentView(R.Layout.pdfview), the view(PDFGraphic) will not be invalidated, if setContentView(New PDFGraphic(this)),it invalidates successfully.
How to refresh the view in the whole layout.
I don't see the problem. These are the results that I get with your code (next time, you do this part). With setContentView(R.layout.pdfview), this is the result:
With setContentView(new PDFGraphic(this, null) -- note that I used the two-arg constructor because you didn't define PDFGraphic(Context) -- this is the result:
In either case, your onDraw() is happening correctly.