具有圆半径的Android进度条动态更改

时间:2012-02-22 12:53:06

标签: android layout view progress-bar geometry

我想根据滑块位置更改圆半径

我是单独创造的。 将circle方法应用于Progress栏。 面临错误。

实际上我用

打电话给Progress-Bar(Seek Bar)
setContentView(R.layout.main);

如您所知,我必须使用setContentView(demoview);

绘制圆圈

查询:我想将两个布局合并为显示图片。

我不知道是否可能?

任何指导,教程都很明显。

感谢您为我的查询留出宝贵的时间。 希望你有解决方案。

enter image description here

3 个答案:

答案 0 :(得分:2)

检查出来.. 我没有使用android搜索栏,而是在画布上绘制类似的搜索栏。 检查此代码是否可以帮助您..

package com.test;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class SampleComp extends View implements OnTouchListener {

    private Paint paint = null;
    private int width = 0;
    private int height = 0;
    private int barStartX = 20;
    private int barStartY = 20;
    private int barEndX;
    private int barEndY = 30;
    private int radius;
    private int heightAvailableForCircle;
    private int widthAvailableForCircle;
    private int maxRadius;
    private int totalSeekBarLength;
    private int currentSeekBarLength = 10;
    private int whatPercentOfSeekBarIsSelected = 50;

    public SampleComp(Context context) {

        super(context);
        paint = new Paint();
        paint.setAntiAlias(true);
        setOnTouchListener(this);
    }
    public SampleComp(Context context, AttributeSet attrs) {

        super(context, attrs);
        paint = new Paint();
        paint.setAntiAlias(true);
    }
    public SampleComp(Context context, AttributeSet attrs, int defStyle) {

        super(context, attrs, defStyle);
        paint = new Paint();
        paint.setAntiAlias(true);
    }

    @Override
    public void onDraw(Canvas canvas){

        barEndX = getWidth() - 20;
        paint.setColor(Color.WHITE);
        setWidth(canvas.getWidth());
        setHeight(canvas.getHeight());

        setHeightAvailableForCircle(getViewHeight() - barEndY);
        setWidthAvailableForCircle(getViewWidth() - 40);
        System.out.println("heightAvailableForCircle: "+getAvailableHeightForCircle());
        System.out.println("widthAvailableForCircle: "+getWidthAvailableForCircle());
        totalSeekBarLength = barEndX - barStartX;
        System.out.println("SEEK LEN: "+totalSeekBarLength);
        canvas.drawRect(barStartX, barStartY, barEndX, barEndY, paint);
        paint.setColor(Color.BLUE);
        setMaxRadius(heightAvailableForCircle, widthAvailableForCircle);
        whatPercentOfSeekBarIsSelected = getSelectedSeekBarPercentage(totalSeekBarLength,getCurrentSeekBarLenghtSelected());
        System.out.println("whatPercentOfSeekBarIsSelected: "+whatPercentOfSeekBarIsSelected);
        System.out.println("!!!!!: "+canvas.getWidth());
        System.out.println("@@@@: "+getViewWidth());
        System.out.println("^^^^^^^^^************: "+ (whatPercentOfSeekBarIsSelected * (getViewWidth() - 40)) / 100);
        canvas.drawRect(barStartX, barStartY, ( (whatPercentOfSeekBarIsSelected * (getViewWidth() - 40)) / 100) + 20,
                barEndY, paint);
        paint.setColor(Color.GRAY);
        setRadius(whatPercentOfSeekBarIsSelected);
        canvas.drawCircle( (canvas.getWidth())/2, (canvas.getHeight() - 30)/2, radius, paint);
    }

    private void setRadius(int per){

        this.radius = (getMaxRadius() * per)/100;
    }
    private int getSelectedSeekBarPercentage(int total, int current){

        int per = 0;
        per = ( (current * 100) / total);
        return per;
    }
    private void setRadius(int total, int current){

        System.out.println("total: "+total);
        System.out.println("current: "+current);
        this.radius = ( ( (getMaxRadius()/2) * current) / 100);
        System.out.println("radius: "+this.radius);
    }
    private void setMaxRadius(int h, int w){

        this.maxRadius = h < w ? h/2 : w/2 ;
    }
    private int getMaxRadius(){

        return this.maxRadius;
    }
    private void setWidth(int w){

        this.width = w;
    }

    private void setHeight(int h){

        this.height = h;
    }

    private int getViewWidth(){

        return this.width;
    }

    private int getViewHeight() {

        return this.height;
    }

    private void setHeightAvailableForCircle(int availableHeightForCircle){

        this.heightAvailableForCircle = availableHeightForCircle;
    }
    private int getAvailableHeightForCircle(){

        return this.heightAvailableForCircle;
    }

    private void setWidthAvailableForCircle(int wid){

        this.widthAvailableForCircle = wid;
    }

    private int getWidthAvailableForCircle(){

        return this.widthAvailableForCircle;
    }
    private void setCurrentSeekBarLength(int x){

        this.currentSeekBarLength = x;
    }
    private int getCurrentSeekBarLenghtSelected(){

        return this.currentSeekBarLength;
    }
    @Override
    public boolean onTouch(View v, MotionEvent event) {

        int x = (int) event.getX();
        int y = (int) event.getY();
        System.out.println("x: "+x);
        System.out.println("y: "+y);

        if(x >= 10 && x<= barEndX && y >= 10 && y <= 30){

            System.out.println("TRUE");
            setCurrentSeekBarLength(x - 20);
            invalidate();
        }
        return false;
    }
}

这是我的Activity类:

package com.test;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.widget.ImageView;

public class SampleActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(new SampleComp(this));
    }
}

答案 1 :(得分:0)

关于你想要达到的目标,你可以更具体一点。你的问题不是很清楚。我能弄清楚的是,你想根据进度条值增加/减少圆的半径。

答案 2 :(得分:0)

查看android custom component guide。您基本上必须从android.view.View派生您的圈子组件。然后,您可以像其他所有组件一样将其添加到布局中。