Search

Wednesday, 7 December 2016

spinner seekbar imageview shapedrawable combine program


Using Spinner select shape name that shap will dislpay in the image view ,based on seek bar shape will increase or decrease


android programs


****  code  ****



XML FILE

<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"
    tools:context=".MainActivity" >

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:entries="@array/shape" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@drawable/ic_launcher"
        
        />

    <SeekBar
        android:id="@+id/seekBar1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/spinner1" />

</RelativeLayout>



JAVA FILE

package com.example.set_1;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.ArcShape;
import android.graphics.drawable.shapes.OvalShape;
import android.graphics.drawable.shapes.RectShape;
import android.graphics.drawable.shapes.Shape;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends Activity {
ImageView img;
Spinner sp;
SeekBar sb;


ShapeDrawable  tr,square;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img=(ImageView)findViewById(R.id.imageView1);
sp=(Spinner)findViewById(R.id.spinner1);
sb=(SeekBar)findViewById(R.id.seekBar1);

registerForContextMenu(img);
 
sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

@Override
public void onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub

}

@Override
public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub


}

@Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
// TODO Auto-generated method stub
//Toast.makeText(getApplicationContext(), ""+arg1, 50).show();
img.getLayoutParams().height=50+arg1;
img.getLayoutParams().width=50+arg1;
String s=sp.getSelectedItem().toString();
if(s.equals("Square")){
ShapeDrawable  square=new ShapeDrawable(new RectShape());
square.setIntrinsicWidth(50);
square.setIntrinsicHeight(50);
square.getPaint().setColor(Color.BLUE);
img.setImageDrawable(square);
Toast.makeText(getApplicationContext(), "Square", 5).show();
}

if(s.equals("Rectangle")){
ShapeDrawable  r=new ShapeDrawable(new RectShape());
r.setIntrinsicWidth(100);
r.setIntrinsicHeight(50);
r.getPaint().setColor(Color.BLUE);
img.setImageDrawable(r);
Toast.makeText(getApplicationContext(), "Rectangle", 5).show();
}
if(s.equals("Triangle")){
tr=new ShapeDrawable(new Shape() {

@Override
public void draw(Canvas arg0, Paint arg1) {
// TODO Auto-generated method stub
int height=(int) (getHeight()/2);
int width=(int) (getWidth()/2);
Point a=new Point(0,0);
Point b=new Point(width,2*height);
Point c=new Point(2*width,0);

Path path=new Path();
path.moveTo(a.x, a.y);
path.lineTo(b.x, b.y);
path.lineTo(c.x, c.y);

arg0.drawPath(path,arg1);
}

});
tr.setIntrinsicWidth(100);
tr.setIntrinsicHeight(100);
tr.getPaint().setColor(Color.BLUE);
img.setImageDrawable(tr);
Toast.makeText(getApplicationContext(), "Triangle", 5).show();
}

if(s.equals("Circle")){
ShapeDrawable  cr=new ShapeDrawable(new OvalShape());
cr.setIntrinsicWidth(100);
cr.setIntrinsicHeight(100);
cr.getPaint().setColor(Color.BLUE);
img.setImageDrawable(cr);
Toast.makeText(getApplicationContext(), "Circle", 5).show();
}
if(s.equals("Arc")){
ShapeDrawable  arc=new ShapeDrawable(new ArcShape(10, 50));
arc.setIntrinsicWidth(100);
arc.setIntrinsicHeight(100);
arc.getPaint().setColor(Color.BLUE);
img.setImageDrawable(arc);
Toast.makeText(getApplicationContext(), "Arc", 5).show();
}
}



});
 
sp.setOnItemSelectedListener(new OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
String s=arg0.getItemAtPosition(arg2).toString();
if(s.equals("Square")){
ShapeDrawable  square=new ShapeDrawable(new RectShape());
square.setIntrinsicWidth(50);
square.setIntrinsicHeight(50);
square.getPaint().setColor(Color.BLUE);
img.setImageDrawable(square);
Toast.makeText(getApplicationContext(), "Square", 5).show();
}

if(s.equals("Rectangle")){
ShapeDrawable  r=new ShapeDrawable(new RectShape());
r.setIntrinsicWidth(100);
r.setIntrinsicHeight(50);
r.getPaint().setColor(Color.BLUE);
img.setImageDrawable(r);
Toast.makeText(getApplicationContext(), "Rectangle", 5).show();
}
if(s.equals("Triangle")){
tr=new ShapeDrawable(new Shape() {

@Override
public void draw(Canvas arg0, Paint arg1) {
// TODO Auto-generated method stub
int height=(int) (getHeight()/2);
int width=(int) (getWidth()/2);
Point a=new Point(0,0);
Point b=new Point(width,2*height);
Point c=new Point(2*width,0);

Path path=new Path();
path.moveTo(a.x, a.y);
path.lineTo(b.x, b.y);
path.lineTo(c.x, c.y);

arg0.drawPath(path,arg1);
}

});
tr.setIntrinsicWidth(100);
tr.setIntrinsicHeight(100);
tr.getPaint().setColor(Color.BLUE);
img.setImageDrawable(tr);
Toast.makeText(getApplicationContext(), "Triangle", 5).show();
}

if(s.equals("Circle")){
ShapeDrawable  cr=new ShapeDrawable(new OvalShape());
cr.setIntrinsicWidth(100);
cr.setIntrinsicHeight(100);
cr.getPaint().setColor(Color.BLUE);
img.setImageDrawable(cr);
Toast.makeText(getApplicationContext(), "Circle", 5).show();
}
if(s.equals("Arc")){
ShapeDrawable  arc=new ShapeDrawable(new ArcShape(10, 50));
arc.setIntrinsicWidth(100);
arc.setIntrinsicHeight(100);
arc.getPaint().setColor(Color.BLUE);
img.setImageDrawable(arc);
Toast.makeText(getApplicationContext(), "Arc", 5).show();
}
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}
});

 
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}


}




you can also modify code as per your choice .

outputoutputoutput


Download Sample Code from here     :====>    Downloaddownload icon


No comments:

Post a Comment