Android RealViewSwitcher : How to start on second page? - android

i'm using RealViewSwitcher I got from this site.
It works perfectly on my code, but I don't have any idea how to set the initial current screen to second or third screen.
the method setCurrentScreen(int) doesn't affect anything and if I change the for loop inside the onLayout() method from for (int i = 0; i < count; i++) {...} into for (int i = 1; i < count; i++){...} It does starts in page two, but you can't go to page one.
Any idea how to start on page two?

The local solution is to add new class method. If you want start on second page - just call snapToScreenFast(2).
public void snapToScreenFast(int whichScreen) {
if (!mScroller.isFinished())
return;
whichScreen = Math.max(0, Math.min(whichScreen, getChildCount() - 1));
mNextScreen = whichScreen;
final int newX = whichScreen * getWidth();
final int delta = newX - getScrollX();
mScroller.startScroll(getScrollX(), 0, delta, 0, 0);
invalidate();
}

Related

check whether there is a circle at a point where user has touched

I have a code where some circles are drawn automatically after passing the image to a function an some are drawn manually using drawcircle(). Now, I want to see that when the user touches a point, if there is already a circle there, then hide/remove it.
Any leads please?
If all circle are drown by you by draw circle . Then store the coordinates of all circles in a list and on then check the the clicked position by Region . If region contains the coordinate then click is inside the circle otherwise its outside somewhere on Canvas.Below is an example it returns the index of clicked circle .
private int findPointAt(int x, int y) {
if(dotsList!=null) {
if (dotsList.isEmpty()) {
return -1;
}
final int width = circleRadius * 2;
final Region r = new Region();
for (int i = 0; i < dotsList.size(); i++) {
final int pointX = dotsList.get(i).x;
final int pointY = dotsList.get(i).y;
r.set(pointX - width, pointY - width, pointX + width, pointY + width);
if (r.contains(x, y)) {
selectedPosition = i;
return i;
}
}
}
return -1;
}
if returns -1 then click is outside otherwise index of circle is returned .

More efficient way to draw lines on canvas?

I'm drawing a grid over a bitmap. The user can change the size, aspect ratio, or drag the grid around. Doing so with drawLines causes fairly bad stuttering when these operations occur. Is there a better approach?
private void drawGrid()
{
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
gridOverlayView.invalidate();
int count = 0;
for(int i = 0; i < numColumns +1 ; i++)
{
points[count] = originX + (cellWidth * i);
points[count+1] = originY;
points[count+2] = originX + (cellWidth * i);
points[count+3] = originY + (gridHeight);
count += 4;
}
for(int i = 0; i < numRows +1; i++)
{
points[count] = originX;
points[count+1] = originY + (cellHeight * i);
points[count+2] = originX + (gridWidth);
points[count+3] = originY + (cellHeight * i);
count += 4;
}
canvas.drawLines(points, 0, points.length, paint);
gridOverlayView.setImageBitmap(gridOverlayBitmap);
}
this is being called from:
#Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
{
originX += e2.getX() - e1.getX();
if(originX < 0f)
{
originX = 0f;
}
if(originX + gridWidth > imageWidth)
{
originX = imageWidth - gridWidth;
}
originY += e2.getY() - e1.getY();
if(originY < 0f)
{
originY = 0f;
}
if(originY + gridHeight > imageHeight)
{
originY = imageHeight - gridHeight;
}
drawGrid();
return true;
}
Since there's no source code you're calling that function, I cannot find the exact reason of stuttering. I guess you want to change the grids in 'real-time' with user interaction.
I think you should avoid redrawing the entire grid while user is dragging.
Instead, you can translate, scale or rotate(if you need) the previous Bitmap as you wish.
Of course the result image will sometimes not in a good quality, but you can recreate the whole grid bitmap when the user stops interacting.
By this way you can achieve 'real-time-like' grid creating effect.
I solved the problem by doing a couple things: Optimizing the layout following the dev guide. Then I simply scaled down the bitmap I was using to draw the grid, then scaled it back up when I wanted to save it onto the original image. That got everything moving nice and smoothly... (actually too fast)

Circular listview android

Im working on my circular list view. I implement almost all functionality, but i have some problem. When my activity starts my listView looks like: https://dl.dropbox.com/u/44444463/Screenshot_2013-02-28-14-37-17.png
But it is not right. It should looks like: https://dl.dropbox.com/u/44444463/Screenshot_2013-02-28-14-37-44.png
Here is my problem.
When activity starts its look like in first screenshot. If i touch any element in listView, or scroll it (if there is a lot of elements) it baceme like in screenshot2. Exactly what i need.
How can i take image like in scrennshot 2 in activity start?
I overrited method #Override public void onScroll(..) in OnScrollListener of my listView, where i aplly all paddings:
// int x = 30;
for (int i = 0; i < visibleItemCount; ++i)
{
View v = listView.getChildAt(i);
if (v != null)
{
LinearLayout rl = (LinearLayout) v.findViewById(R.id.rl);
float sin = (float) (v.getTop() + v.getHeight() / 2 - centerOfCircle) / radius;
double angle = Math.asin(sin);
int x = (int) (-dXRow + radius * Math.cos(angle));
if (x < 0)
x = 0;
Log.d("MN", "setPadding: " + x);
rl.setPadding(x, 0, 0, 0);
}
}
Really sorry for my Engish
You should set starting peddings in the getView() method of your listView's adapter. And then recompute them onScroll() as you need.
OnScroll method isnt invoked when the listView appear, only when you scroll it.

Android:Scrollbar for running graph

I've a custom view which draws a running graph - some magnitude versus time. Now I want to implement a custom scroll bar for this so that I can view past data which are offscreen. The data is available to me. I just need the %offset selection by the user.
Any help/suggestions on implementation would be v helpful.
Code Snippet from my Custom view's onDraw method
public void onDraw(Canvas canvas) {
int totalpts = data.size();
scale = getWidth() / (float) maxpoints;
List<Data> display = new ArrayList<Data>();
int initial = 1;
if (totalpts > maxpoints) {
initial = totalpts - maxpoints;
display = data.subList(initial, data.size() - 1);
} else {
display = data;
}
int size = display.size();
Data start = null;
float x1 = 0, x2 = 0, x = 0;
if (size > 1) {
x1 = getWidth();
start = display.get(display.size() - 1);
for (int i = display.size() - 1; i >= 0; i--) {
Data stop = display.get(i);
x = x1;
x1 -= (stop.x * scale / 1000);
canvas.drawLine(x, start.Y, x1, stop.Y, paint1);
start = stop;
}
}
}
Try putting your custom control inside a HorizonatalScrollView (assuming you want it to scroll horizontally), use ScrollView otherwise), setting the width of your control to "WRAP_CONTENT", and the HoizontalScrollView to "FILL_PARENT". Without seeing the code for your custom view, it's difficult to know whether you might need to do some tinkering with the width calculation to get this working.

onDraw() triggered but results don't show

I have the following routine in a subclass of view:
It calculates an array of points that make up a line, then erases the previous lines, then draws the new lines (impact refers to the width in pixels drawn with multiple lines). The line is your basic bell curve, squeezed or stretched by variance and x-factor.
Unfortunately, nothing shows on the screen. A previous version with drawPoint() and no array worked, and I've verified the array contents are being loaded correctly, and I can see that my onDraw() is being triggered.
Any ideas why it might not be drawn? Thanks in advance!
protected void drawNewLine( int maxx, int maxy, Canvas canvas, int impact, double variance, double xFactor, int color) {
// impact = 2 to 8; xFactor between 4 and 20; variance between 0.2 and 5
double x = 0;
double y = 0;
int cx = maxx / 2;
int cy = maxy / 2;
int mu = cx;
int index = 0;
points[maxx<<1][1] = points[maxx<<1][0];
for (x = 0; x < maxx; x++) {
points[index][1] = points[index][0];
points[index][0] = (float) x;
Log.i(DEBUG_TAG, "x: " + x);
index++;
double root = 1.0 / (Math.sqrt(2 * Math.PI * variance));
double exponent = -1.0 * (Math.pow(((x - mu)/maxx*xFactor), 2) / (2 * variance));
double ePow = Math.exp(exponent);
y = Math.round(cy * root * ePow);
points[index][1] = points[index][0];
points[index][0] = (float) (maxy - y - OFFSET);
index++;
}
points[maxx<<1][0] = (float) impact;
for (int line = 0; line < points[maxx<<1][1]; line++) {
for (int pt = 0; pt < (maxx<<1); pt++) {
pointsToPaint[pt] = points[pt][1];
}
for (int skip = 1; skip < (maxx<<1); skip = skip + 2)
pointsToPaint[skip] = pointsToPaint[skip] + line;
myLinePaint.setColor(Color.BLACK);
canvas.drawLines(pointsToPaint, bLinePaint); // draw over old lines w/blk
}
for (int line = 0; line < points[maxx<<1][0]; line++) {
for (int pt = 0; pt < maxx<<1; pt++) {
pointsToPaint[pt] = points[pt][0];
}
for (int skip = 1; skip < maxx<<1; skip = skip + 2)
pointsToPaint[skip] = pointsToPaint[skip] + line;
myLinePaint.setColor(color);
canvas.drawLines(pointsToPaint, myLinePaint); / new color
}
}
update: Replaced the drawLines() with drawPoint() in loop, still no joy
for (int p = 0; p<pointsToPaint.length; p = p + 2) {
Log.i(DEBUG_TAG, "x " + pointsToPaint[p] + " y " + pointsToPaint[p+1]);
canvas.drawPoint(pointsToPaint[p], pointsToPaint[p+1], myLinePaint);
}
/// canvas.drawLines(pointsToPaint, myLinePaint);
I was attempting to write from within onCreate() and onStart(). The View and its Canvas are never actually rendered for the first time until the end of onStart().
aren't you suppose to call invalidate (like a mapview) to force the view to reload?
YourView.invalidate() (or maybe postInvalidate(), depending where you are : main sthread or not)
here is the detail

Categories

Resources