---------- Forwarded message ----------
From: *kavitha b* <[email protected] <mailto:[email protected]>>
Date: 2010/11/10
Subject: Re: [android-developers] nest custom views
To: [email protected]
<mailto:[email protected]>
Hi Kumar,
my requirement is
I have a custom scrollview where i am scrolling a image.
That fills up half screen.
I want to move images upon entire screen,including upon custom
scrollview also.
I cannot draw bitmaps using onDraw() because I cannot draw in entire
screen.
How to move images now.
I wiill attach screenshot also for refernce.I want to move lettered
tiles upon board and board is scrollview here.
My code is:
public class HorizoontalScroll extends Activity implements
OnGestureListener
{
private static Vector<Square> squares;
private static Vector<Tile> tiles;
private Tile tile;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
squares=Util.generateBoardSquares();
tiles=Util.generateTiles(this);
}
}
main.xml looks like this
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/mylayout"
>
<com.horizonscroll.ScrollImageView
android:id = "@+id/scrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</AbsoluteLayout>
And ScrollImageView.java
public class ScrollImageView extends View {
private final int DEFAULT_PADDING = 0;
private Display mDisplay;
private Bitmap mImage;
private Bitmap mTile;
/* Current x and y of the touch */
private float mCurrentX = 0;
private float mCurrentY = 0;
private float mTotalX = 0;
private float mTotalY = 0;
/* The touch distance change from the current touch */
private float mDeltaX = 0;
private float mDeltaY = 0;
int mDisplayWidth;
int mDisplayHeight;
int mPadding;
public ScrollImageView(Context context) {
super(context);
initScrollImageView(context);
}
public ScrollImageView(Context context, AttributeSet attributeSet) {
super(context);
initScrollImageView(context);
}
private void initScrollImageView(Context context) {
mDisplay =
((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
mPadding = DEFAULT_PADDING;
mImage=BitmapFactory.decodeResource(getResources(),
R.drawable.woodstandardboard);
mTile=BitmapFactory.decodeResource(getResources(),
R.drawable.woodtile);
}
@Override
protected void onMeasure(int widthMeasureSpec, int
heightMeasureSpec) {
int width = measureDim(widthMeasureSpec, mDisplay.getWidth());
int height = measureDim(heightMeasureSpec, mDisplay.getHeight());
setMeasuredDimension(width, height-100);
}
private int measureDim(int measureSpec, int size) {
int result = 0;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
if (specMode == MeasureSpec.EXACTLY) {
result = specSize;
} else {
result = size;
if (specMode == MeasureSpec.AT_MOST) {
result = Math.min(result, specSize);
}
}
return result;
}
public Bitmap getImage() {
return mImage;
}
public void setImage(Bitmap image) {
mImage = image;
}
public int getPadding() {
return mPadding;
}
public void setPadding(int padding) {
this.mPadding = padding;
}
private static int boardX=0;
private static int boardY=0;
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
mCurrentX = event.getRawX();
mCurrentY = event.getRawY();
}
else if (event.getAction() == MotionEvent.ACTION_MOVE) {
float x = event.getRawX();
float y = event.getRawY();
// Update how much the touch moved
mDeltaX = x - mCurrentX;
mDeltaY = y - mCurrentY;
mCurrentX = x;
mCurrentY = y;
invalidate();
}
// Consume event
return true;
}
@Override
protected void onDraw(Canvas canvas) {
if (mImage == null) {
return;
}
float newTotalX = mTotalX + mDeltaX;
// Don't scroll off the left or right edges of the bitmap.
if (mPadding > newTotalX && newTotalX > getMeasuredWidth() -
mImage.getWidth() - mPadding)
mTotalX += mDeltaX;
float newTotalY = mTotalY + mDeltaY;
// Don't scroll off the top or bottom edges of the bitmap.
if (mPadding > newTotalY && newTotalY > getMeasuredHeight() -
mImage.getHeight() - mPadding)
mTotalY += mDeltaY;
Paint paint = new Paint();
canvas.drawBitmap(mImage, mTotalX, mTotalY, paint);
canvas.drawBitmap(mTile, mTotalX+50, mTotalY+500, paint);
canvas.drawBitmap(mTile, mTotalX+50, mTotalY+50, paint);
}
}
On Wed, Nov 10, 2010 at 10:29 PM, Kumar Bibek <[email protected]
<mailto:[email protected]>> wrote:
Well, if you mean nesting View classes, that's not possible. You
can nest views/viewgroups if the parent is a ViewGroup.
2010/11/10 kavitha b <[email protected]
<mailto:[email protected]>>
I want nest 2 custom views through code.
I didnt understand how to do it.
Please provide any example.
2010/11/10 Kostya Vasilyev <[email protected]
<mailto:[email protected]>>
Well, subclass ViewGroup or one of its subclasses for the
outer view, and View or one of its subclasses for the
inner view. Then create them nested, either with xml, or
from code.
--
Kostya Vasilyev -- http://kmansoft.wordpress.com
10.11.2010 19:04 пользователь "kavitha b"
<[email protected] <mailto:[email protected]>>
написал:
Can you please provide me an example of that.
I want to nest custom views,,,i mean outside one custom
view and inside that another custom view.but how to draw
a view within another?
It is very very urgent.
Please help.
2010/11/10 Kostya Vasilyev <[email protected]
<mailto:[email protected]>>
>
> Sure, as long as the enclosing view is a ViewGroup
(custom or not).
>
> --
> Kostya Vasilyev --...
--
You received this message because you are subscribed to
the Google
Groups "Android Developers...
--
You received this message because you are subscribed to
the Google
Groups "Android Developers" group.
To post to this group, send email to
[email protected]
<mailto:[email protected]>
To unsubscribe from this group, send email to
[email protected]
<mailto:android-developers%[email protected]>
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to
[email protected]
<mailto:[email protected]>
To unsubscribe from this group, send email to
[email protected]
<mailto:android-developers%[email protected]>
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--
Kumar Bibek
http://techdroid.kbeanie.com
http://www.kbeanie.com
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to
[email protected]
<mailto:[email protected]>
To unsubscribe from this group, send email to
[email protected]
<mailto:android-developers%[email protected]>
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en