View setBackgroundColor not working android - android

I have a problem with setting background color of a view (Note: all elements are View)
public class MainActivity extends ActionBarActivity {
final Context context = this;
//Original Colors (Hardcodeded for nice looks)
int r1c = Color.parseColor("#ff00eeff");
int r2c = Color.parseColor("#ff21ff2e");
//Final Colors Random
int r1f = randomColor();
int r2f = randomColor();
//Get int for progress
int progress = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final View r1 = (View) findViewById(R.id.r1);
final View r2 = (View) findViewById(R.id.r2);
final SeekBar bar = (SeekBar) findViewById(R.id.seekBar);
bar.setProgress(0);
bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) {}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {}
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
progress = bar.getProgress();
updateColors(r1,r1c,r1f);
updateColors(r2,r2c,r2f);
//updateColors(r1,r1c,r1f); Grey in this case TODO automatic
}
});
}
public void updateColors(View v, int ogcolor, int fcolor) {
ArgbEvaluator getColorAtPos = new ArgbEvaluator();
float percent = (float) progress / 100; //Does this return float expected between 0.0 and 1.0 ???? Im new to java so not sure
v.setBackgroundColor((Integer) getColorAtPos.evaluate(percent, ogcolor, fcolor));
v.invalidate();
}
int randomColor() {
Random rand = new Random();
int r = (int) rand.nextInt(255);
int g = (int) rand.nextInt(255);
int b = (int) rand.nextInt(255);
int color = Color.argb(255,r, g, b);
return color;
}
}
I have described most in comments in code, but once more, when I move seekBar, onProgressChanged is called successfully, also updatecolors is called, yet I can't see the color changing on activity.

ok got it , GetProgress() was not working as expected , so value of process was always 0 , (int progress was defined at start of onCreate and it is also in args of onProgressChanged so there was conflict ,but i wonder why AS did not warn me about same var name twice

Related

call method with multiple parameters in onClick() of a button

I am new to Android and I am trying to Build a gallery app with Edit Activity. I have attached the screenshot of edit screen of my app. There is an ImageView, a SeekBar and 4 ImageButtons. The ImageButtons implements 4 edit functionality-Brightness,saturation and etc.. I have all methods for effects. All I want to know is when I click the imageButton(may be brightness), and drag the seekbar, the brightness should increase and similarly, when i click Contrast ImageButton and drag the seekbar, Contrast of Image should Increase. How can I implement it. Could Someone Please help me with it.. I tried using setOnTouchListener() for ImageButtons but that dint work, as it accepts only view as parameter and not Bitmap. Please help me.Thanks in Advance
Below is my Edit Activity
public class EditActivity extends Activity
{
private Bitmap bitmap;
private ImageView image;
private ImageButton bb,sab,shb,cb;
private BitmapDrawable drawable;
private SeekBar seekBar;
#Override
public void onCreate( Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.filters);
// Get intent data
Intent i = getIntent();
// Selected image id
final int position = i.getExtras().getInt("id");
ImageAdapter imageAdapter = new ImageAdapter(this);
image = (ImageView) findViewById(R.id.imageView);
image.setImageResource(imageAdapter.ids[position]);
bb = (ImageButton) findViewById(R.id.brightButton);
drawable = (BitmapDrawable) image.getDrawable();
bitmap = drawable.getBitmap();
bb.setImageBitmap(bitmap);
sab=(ImageButton)findViewById(R.id.saturationButton);
drawable = (BitmapDrawable) image.getDrawable();
bitmap = drawable.getBitmap();
sab.setImageBitmap(bitmap);
cb=(ImageButton) findViewById(R.id.contrastButton);
drawable = (BitmapDrawable) image.getDrawable();
bitmap = drawable.getBitmap();
cb.setImageBitmap(bitmap);
shb=(ImageButton) findViewById(R.id.sharpButton);
drawable = (BitmapDrawable) image.getDrawable();
bitmap = drawable.getBitmap();
shb.setImageBitmap(bitmap);
SeekBar seekBar=(SeekBar)findViewById(R.id.seekBar);
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
int brightness;
#Override
public void onStopTrackingTouch(SeekBar arg0) {
}
#Override
public void onStartTrackingTouch(SeekBar arg0) {
}
#Override
public void onProgressChanged(SeekBar arg0, final int progress, boolean arg2)
{
brightness = progress;
Bitmap bBitMap = brightClicked(bitmap,progress);
image.setImageBitmap(bBitMap);
Bitmap saBitMap = sharpClicked(bitmap,progress);
image.setImageBitmap(saBitMap);
Bitmap cBitMap = saturationClicked(bitmap,progress);
image.setImageBitmap(cBitMap);
Bitmap shBitMap = contrastClicked(bitmap,progress);
image.setImageBitmap(shBitMap);
}
});
}
public Bitmap brightClicked(Bitmap bbitmap,int value)
{
Bitmap bmOut= Bitmap.createBitmap(bbitmap.getWidth(), bbitmap.getHeight(),bbitmap.getConfig());
int A, R, G, B;
int pixel;
for(int i=0; i<bbitmap.getWidth(); i++){
for(int j=0; j<bbitmap.getHeight(); j++)
{
pixel = bbitmap.getPixel(i, j);
A = Color.alpha(pixel);
R = Color.red(pixel);
G = Color.green(pixel);
B = Color.blue(pixel);
R += value;
if (R > 255)
{
R = 255;
} else if (R < 0)
{
R = 0;
}
G += value;
if (G > 255)
{
G = 255;
} else if (G < 0)
{
G = 0;
}
B += value;
if (B > 255)
{
B = 255;
} else if (B < 0)
{
B = 0;
}
bmOut.setPixel(i, j, Color.argb(A, R, G, B));
}
}
return bmOut;
}// and other methods of effects like sharpness,contrast..
Below is Screenshot of EditImage
I would begin with defining state so that the SeekBar can quickly determine which attribute it should be modifying (contrast, brightness, sharpness, saturation). Throw these into an enum class (Google recommends avoiding enums in Android) if you like or define them within the class itself as ints. For example:
public class EditActivity extends Activity {
private int currentState;
private static final int SATURATE = 0;
private static final int CONTRAST = 1;
....
}
Now you can just have the state be updated based on the button clicked:
bb = (ImageButton) findViewById(R.id.brightButton);
bb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
currentState = BRIGHTNESS;
}
}
Then use a switch-case statement to quickly determine the state of the Activity and adjust the image accordingly through your image methods.
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
#Override
public void onProgressChanged(SeekBar arg0, final int progress, boolean arg2)
{
brightness = progress;
switch (currentState) {
case (SATURATE) :
image.setImageBitmap(saturationClicked(bitmap,progress));
break;
case (CONTRAST) :
image.setImageBitmap(contrastClicked(bitmap,progress));
break;
It looks like your current implementation would run all your filters on the image any time the seek bar is moved, which I imagine would cause horrible performance. A switch case would just help you avoid that.

Android Seekbar with max progress != max value

I'd like to set a max progress value in a Seekbar that is not the same as the max value.
For example max would be 100 and max progress 50, so the progress slider could only be dragged to the middle. Is there a way to do that?
You can try the following
int seekBarMaxValue = 100;
int seekBarDraggableMaxValue = 50;
seekBar.setMax(seekBarMaxValue );
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangetListener() {
public void onProgressChanged (SeekBar seekBar, int progress, boolean fromUser) {
seekBar.setProgress(progress > seekBarDraggableMaxValue ? seekBarDraggableMaxValue : progress);
}
});
Hope it helps.
It seems like you want three numbers only (ex. 0; 50; 100); just pretend each number is represents another number or multiply the progress by the common divisor:
Example
0 = 0 or (0 * 50)
1 = 50 or (1 * 50)
2 = 100 or (2 * 50)
int int_actual_number = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
...
seek_sample.setMax(2);
seek_sample.setProgress(0);
seek_sample.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
int_actual_number = progress * 50;
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
You can use different common divisors and amount of numbers to test this method for your needs.

Custom ProgressBar

I am trying to create a ProgresBar that looks like the following:
So far, I have created an object which extends ProgressBar, and now I am trying to figure out what my next step is.
I know that I need to override onDraw() with some logic that will decide how many squares to color in. This is trivial.
What I don't know how to do is get these squares in the first place. How can I replace the default drawable, so when I add my custom bar in the layout I can see something like my image?
try this custom Drawable:
class ProgressDrawable extends Drawable {
private static final int NUM_RECTS = 10;
Paint mPaint = new Paint();
#Override
protected boolean onLevelChange(int level) {
invalidateSelf();
return true;
}
#Override
public void draw(Canvas canvas) {
int level = getLevel();
Rect b = getBounds();
float width = b.width();
for (int i = 0; i < NUM_RECTS; i++) {
float left = width * i / NUM_RECTS;
float right = left + 0.9f * width / NUM_RECTS;
mPaint.setColor((i + 1) * 10000 / NUM_RECTS <= level? 0xff888888 : 0xffbbbbbb);
canvas.drawRect(left, b.top, right, b.bottom, mPaint);
}
}
#Override
public void setAlpha(int alpha) {
}
#Override
public void setColorFilter(ColorFilter cf) {
}
#Override
public int getOpacity() {
return PixelFormat.TRANSLUCENT;
}
}
and test it with the following in onCreate:
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
final ProgressBar pb = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal);
Drawable d = new ProgressDrawable();
pb.setProgressDrawable(d);
pb.setPadding(20, 20, 20, 0);
ll.addView(pb);
OnSeekBarChangeListener l = new OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
int newProgress = pb.getMax() * progress / seekBar.getMax();
Log.d(TAG, "onProgressChanged " + newProgress);
pb.setProgress(newProgress);
}
};
int[] maxs = {4, 10, 60, 110};
for (int i = 0; i < maxs.length; i++) {
SeekBar sb = new SeekBar(this);
sb.setMax(maxs[i]);
sb.setOnSeekBarChangeListener(l);
sb.setPadding(20, 20, 20, 0);
ll.addView(sb);
}
setContentView(ll);

android horizontal scrollview working with seekbar

In my app I have a horizontal scrollview of 6 duck images, while the horizontal scrollview can work properly, now I would like to add a SeekBar underneath the horizontal scrollview such that moving the seekbar will positioning the horizontal scrollview to the proper item.
I have written the codes as follows:
Seekbar:
seekBar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
{
#Override
public void onStopTrackingTouch(SeekBar seekBar)
{
// TODO Auto-generated method stub
}
#Override
public void onStartTrackingTouch(SeekBar seekBar)
{
// TODO Auto-generated method stub
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
seekBar.setProgress(progress);
if (progress ==5) {mLinearLayout.scrollTo(R.drawable.d_duck400_5, 0);}
if (progress ==4) {mLinearLayout.scrollTo(R.drawable.d_duck400_4, 0);}
if (progress ==3) {mLinearLayout.scrollTo(R.drawable.d_duck400_3, 0);}
if (progress ==2) {mLinearLayout.scrollTo(R.drawable.d_duck400_2, 0);}
if (progress ==1) {mLinearLayout.scrollTo(R.drawable.d_duck400_1, 0);}
if (progress ==0) {mLinearLayout.scrollTo(R.drawable.d_duck400_0, 0);}
}
});
}
Horizontal Scrollview:
private void getDeviceWidth()
{
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
mWidth = dm.widthPixels;
}
private void initView()
{
int[] imageArray = { R.drawable.d_duck400_0, R.drawable.d_duck400_1, R.drawable.d_duck400_2,
R.drawable.d_duck400_3, R.drawable.d_duck400_4, R.drawable.d_duck400_5};
// int[] imageArray = { R.drawable.d_duck1_400, R.drawable.d_duck1_400, R.drawable.d_duck1_400,
// R.drawable.d_duck1_400, R.drawable.d_duck1_400, R.drawable.d_duck1_400};
mLinearLayout = (LinearLayout) findViewById(R.id.scrollview_layout);
mLinearLayout.removeAllViews();
for (int i = 0; i < 6; i++)
{// Place 6 items into horizontalscrollview
int width = mWidth /1;
LinearLayout itemLayout = (LinearLayout) LinearLayout.inflate(Duck.this, R.layout.d_scrollview_item, null);
itemLayout.setLayoutParams(new LayoutParams(width, LayoutParams.MATCH_PARENT));// set the width as 1 screen showing 3 items
itemLayout.setGravity(Gravity.CENTER_VERTICAL);
ImageView mImageView = (ImageView) itemLayout.findViewById(R.id.imageview);
TextView mTextView = (TextView) itemLayout.findViewById(R.id.textview);
final String page = "The" + (i + 1) + "th page";
mTextView.setText(page);
mImageView.setBackgroundResource(imageArray[i]);
mLinearLayout.addView(itemLayout);
itemLayout.setOnTouchListener(new OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event)
{
// Toast.makeText(Duck.this, "Click" + page, Toast.LENGTH_SHORT).show();
return false;
}
});
}
}
Question:
Once I move the seekbar, the pictures in the horizontal Scrollview all gone.
What is happening? Thanks in advance for your advice!
if (progress ==5) {mLinearLayout.scrollTo(R.drawable.d_duck400_5, 0);
scrollTo expects x,y coordinates to scroll to a new position. But you give it R.drawable.d_duck400_5 as x value. This will be just a random integer produced by compiler. It is not the x coordinate you want to scroll.
Try something like this:
if (progress ==5) {mLinearLayout.scrollTo(200, 0);
if (progress ==5) {mLinearLayout.scrollTo(400, 0);
....
If you want to find the position of a view, you can go with getLocationInWindow or getLocationOnScreen methods.
int[] location = new int[2];
yourView.getLocationOnScreen(location);
int x = location[0];
int y = location[1];

Android seekbar with custom thumb having dynamic text inside it

I want to create android custom SeekBar having thumb with text inside it to show current seek position.
Here is my code:
SeekBar sb;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_seek_bar_activity);
sb = (SeekBar)findViewById(R.id.slider);
sb.setMax(100);
sb.setProgress(10);
BitmapDrawable bd = writeOnDrawable(R.drawable.star2, Double.toString(50));
sb.setThumb(bd);
sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
int pos = sb.getProgress();
double star = pos/(20.0);
TextView tv = (TextView)findViewById(R.id.percent);
tv.setText(Double.toString(star)+"%");
BitmapDrawable bd = writeOnDrawable(R.drawable.star2, Double.toString(star));
bd.setBounds(new Rect(0,0,
bd.getIntrinsicWidth(),
bd.getIntrinsicHeight()
));
seekBar.setThumb(bd);
}
#Override
public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
#Override
public void onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
});
}
public BitmapDrawable writeOnDrawable(int drawableId, String text){
Bitmap bm = BitmapFactory.decodeResource(getResources(), drawableId).copy(Bitmap.Config.ARGB_8888, true);
Paint paint = new Paint();
paint.setStyle(Style.FILL);
paint.setColor(Color.BLACK);
paint.setTextSize(10);
Canvas canvas = new Canvas(bm);
canvas.drawText(text, 0, bm.getHeight()/2, paint);
return new BitmapDrawable(bm);
}
but when I move thumb it goes to the beginning of the seek bar.
Does anyone have solution to move custom thumb with seekbar position?
I use SeekBar to display a timer countdown in my app. Inside the timer thumb I show the current SeekBar progress number using the below code:
SeekBar timerBar = (SeekBar) findViewById(R.id.seekBarTimer);
if (timerBar != null) {
timerBar.setMax((int) (Settings.countdownSeconds + 1));
timerBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar arg0) {
}
#Override
public void onStartTrackingTouch(SeekBar arg0) {
}
#Override
public void onProgressChanged(SeekBar timerBar, int arg1, boolean arg2) {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.seek_thumb);
Bitmap bmp = bitmap.copy(Bitmap.Config.ARGB_8888, true);
Canvas c = new Canvas(bmp);
String text = Integer.toString(timerBar.getProgress());
Paint p = new Paint();
p.setTypeface(Typeface.DEFAULT_BOLD);
p.setTextSize(14);
p.setColor(0xFFFFFFFF);
int width = (int) p.measureText(text);
int yPos = (int) ((c.getHeight() / 2) - ((p.descent() + p.ascent()) / 2));
c.drawText(text, (bmp.getWidth()-width)/2, yPos, p);
timerBar.setThumb(new BitmapDrawable(getResources(), bmp));
}
});
timerBar.setProgress(0);
}
The R.drawable.seek_thumb drawable is my thumb drawable.
I got solution now, in setBound() method I was passing top left as 0, that's why it is showing seek bar at beginning. After doing following change I got it works.
Call setThumbPos() method in onProgressChanged() event
public void setThumbPosition(SeekBar seekBar){
int max = seekBar.getMax();
int available = seekBar.getWidth() - seekBar.getPaddingLeft() - seekBar.getPaddingRight();
float scale = max > 0 ? (float) seekBar.getProgress() / (float) max : 0;
//scale = 1;
int pos = sb.getProgress();
double star = pos/(20.0);
BitmapDrawable bd = writeOnDrawable(R.drawable.star2, Double.toString(star));
int thumbWidth = bd.getIntrinsicWidth();
int thumbHeight = bd.getIntrinsicHeight();
//available -= thumbWidth;
int thumbPos = (int) (scale * available);
if(thumbPos <= 0+thumbWidth){
thumbPos += (thumbWidth/2);
}else if(thumbPos >= seekBar.getWidth()-thumbWidth){
thumbPos -= (thumbWidth/2);
}
bd.setBounds(new Rect(thumbPos,0,
thumbPos+bd.getIntrinsicWidth(),
bd.getIntrinsicHeight()
));
seekBar.setThumb(bd);
TextView tv = (TextView)findViewById(R.id.percent);
tv.setText(Double.toString(star)+"%");
}
I ended up using this simple solution. Its probably not as high-performance as say a proper custom SeekBar, however its really easy to plug into an existing layout, and use any label or other view on top of the SeekBar thumb.
protected void positionThumbLabel(SeekBar seekBar, TextView label)
{
Rect tr = seekBar.getThumb().getBounds();
label.setWidth(tr.width());
label.setX(tr.left + seekBar.getPaddingLeft());
}
With some minor changes you can position an overlay relative to the center of the thumb:
protected void positionThumbOverlayCenter(SeekBar seekBar, View overlay)
{
Rect tr = seekBar.getThumb().getBounds();
overlay.setX(tr.centerX() - (overlay.getWidth() * 0.5f) + seekBar.getPaddingLeft());
}
Pick a solution that match your situation here: http://www.helptouser.com/code/10722746-add-dynamic-text-over-android-seekbar-thumb.html

Categories

Resources