This are very simple to develop for nine patch image or button or any other in android.
So i create nine patch simple example and this example is very useful for any other things.
This example code is given below.
Please Download This PNG File And give the name of this PNG file is button.9 It is very
important.
main.xml
<?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">
<TableLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:stretchColumns="1">
<TableRow android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_gravity="center_vertical"
android:text="Horizontal:" />
<SeekBar android:id="@+id/horizontal" android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</TableRow>
<TableRow android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_gravity="center_vertical"
android:text="Vertical:" />
<SeekBar android:id="@+id/vertical" android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</TableRow>
</TableLayout>
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<Button android:id="@+id/resize" android:layout_width="64px"
android:layout_height="64px" android:text="Hello" android:textSize="5pt"
android:background="@drawable/button" />
</LinearLayout>
</LinearLayout>
Now coding part of java file .
NinePatch.java
package com.android;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.SeekBar;
public class NinePatchDemo extends Activity {
SeekBar horizontal = null;
SeekBar vertical = null;
View thingToResize = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
thingToResize = findViewById(R.id.resize);
horizontal = (SeekBar) findViewById(R.id.horizontal);
vertical = (SeekBar) findViewById(R.id.vertical);
horizontal.setMax(176); // 240 less 64 starting size
vertical.setMax(176); // keep it square @ max
horizontal.setOnSeekBarChangeListener(h);
vertical.setOnSeekBarChangeListener(v);
}
SeekBar.OnSeekBarChangeListener h = new SeekBar.OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromTouch) {
ViewGroup.LayoutParams old = thingToResize.getLayoutParams();
ViewGroup.LayoutParams current = new LinearLayout.LayoutParams(
64 + progress, old.height);
thingToResize.setLayoutParams(current);
}
public void onStartTrackingTouch(SeekBar seekBar) {
// unused
}
public void onStopTrackingTouch(SeekBar seekBar) {
// unused
}
};
SeekBar.OnSeekBarChangeListener v = new SeekBar.OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromTouch) {
ViewGroup.LayoutParams old = thingToResize.getLayoutParams();
ViewGroup.LayoutParams current = new LinearLayout.LayoutParams(
old.width, 64 + progress);
thingToResize.setLayoutParams(current);
}
public void onStartTrackingTouch(SeekBar seekBar) {
// unused
}
public void onStopTrackingTouch(SeekBar seekBar) {
// unused
}
};
}
And This Layout output given below.
2 comments:
its working G8!!!
Well Done!!
I love 9 patch PNGS :)
Small in size, looks great on android, support for many screen size and orientation...
Get some free 9 patch PNGs on my blog for any of your android projects, including commercial apps.
http://android9patch.blogspot.com/
Cheers!
Post a Comment