Custom, refreshing view not responding to OnClick - android

I made a view that contains a bar graph, which for now is fueled by random numbers generator and refreshes once a second. The idea is that after clicking one of the bars, it would change to a line graph.
This is code of the view:
public class BarGraph extends View implements OnClickListener{
private Paint paint;
private String[] horlabels;
private String[] verlabels;
private String title;
private int[] values;
Context co;
private RectF[] rects;
public BarGraph(Context context, String title, String[] hor, String[] var) {
super(context);
// TODO Auto-generated constructor stub
if (title == null)
title = "";
else
this.title = title;
if (hor == null)
this.horlabels = new String[0];
else
this.horlabels = hor;
if (var == null)
this.verlabels = new String[0];
else
this.verlabels = var;
paint = new Paint();
co = context;
}
public void updateVals(int[] vals){
values = vals;
}
#Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
float border = 20;
float horstart = border;
float height = getHeight();
float width = getWidth() - 1;
int max = 100;
int min = 0;
float diff = max - min;
float graphheight = height - (2 * border);
float graphwidth = width - (2 * border);
rects = new RectF[horlabels.length];
this.setClickable(true);
paint.setTextAlign(Align.LEFT);
int vers = verlabels.length - 1;
for (int i = 0; i < verlabels.length; i++) {
paint.setColor(Color.DKGRAY);
float y = ((graphheight / vers) * i) + border;
canvas.drawLine(horstart, y, width - border, y, paint);
paint.setColor(Color.WHITE);
canvas.drawText(verlabels[verlabels.length - 1 - i], 0, y, paint);
}
int hors = horlabels.length;
for (int i = 0; i < horlabels.length + 1; i++) {
paint.setColor(Color.DKGRAY);
float x = ((graphwidth / hors) * i) + horstart;
canvas.drawLine(x, height - border, x, border, paint);
paint.setTextAlign(Align.CENTER);
if (i==horlabels.length-1)
paint.setTextAlign(Align.RIGHT);
if (i==0)
paint.setTextAlign(Align.LEFT);
paint.setColor(Color.WHITE);
}
for (int i = 0; i < horlabels.length; i++)
{
float x = ((graphwidth / hors) * i) + horstart;
canvas.drawText(horlabels[i], x + (graphwidth / (2*hors)), height - 4, paint);
}
paint.setTextAlign(Align.CENTER);
canvas.drawText(title , (graphwidth / 2) + horstart, border - 4, paint);
if (max != min) {
paint.setColor(Color.LTGRAY);
float datalength = values.length;
float colwidth = (width - (2 * border)) / datalength;
for (int i = 0; i < values.length; i++) {
float val = values[i] - min;
float rat = val / diff;
float h = graphheight * rat;
RectF r = new RectF((i * colwidth) + horstart, (border - h) + graphheight, ((i * colwidth) + horstart) + (colwidth - 1), height - (border - 1));
rects[i] = r;
canvas.drawRect(r, paint);
}
}
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int[] coords = new int[2];
v.getLocationInWindow(coords);
Toast t = Toast.makeText(co, "KLIKNIETO", Toast.LENGTH_LONG);
t.show();
for(RectF r : rects){
if(r.contains(coords[1], coords[2]))
{
try {
this.finalize();
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return;
}
It's started in this activity:
public class GraphTestActivity extends Activity{
/** Called when the activity is first created. */
public List<DataSet> sets;
String[] verlabels;
int[] vals;
BarGraph graph;
Thread t1;
String[] hortable;
// private Updater task;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sets = new LinkedList<DataSet>();
verlabels = new String[] {"0", "10", "20", "30", "40", "50", "60", "70", "80", "90", "100"};
DataSet set1 = new DataSet("set1");
DataSet set2 = new DataSet("set2");
DataSet set3 = new DataSet("set3");
DataSet set4 = new DataSet("set4");
DataSet set5 = new DataSet("set5");
sets.add(set1);
sets.add(set2);
sets.add(set3);
sets.add(set4);
sets.add(set5);
hortable = new String[sets.size()];
vals = new int[sets.size()];
for(int i = 0; i < sets.size(); i++)
{
hortable[i] = sets.get(i).getName();
}
graph = new BarGraph(this, "Diagram slupkowy", hortable, verlabels);
for(DataSet d : sets)
{
d.nextGen();
}
graph.updateVals(vals);
setContentView(graph);
new Updater().execute();
}
#Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
new BarRefresh().execute();
}
class Updater extends AsyncTask<Void, Void, Void>{
#Override
protected Void doInBackground(Void... params) {
// doSomething();
while(true){
for(DataSet d : sets)
{
d.nextGen();
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(isCancelled()){
break;
}
}
return null;
}
}
class BarRefresh extends AsyncTask<Void, Void, Void>{
#Override
protected Void doInBackground(Void... params) {
// doSomething();
while(true){
for(int i = 0; i < sets.size(); i++)
{
vals[i] = sets.get(i).getLast();
}
graph.updateVals(vals);
graph.postInvalidate();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(isCancelled()){
break;
}
}
return null;
}
}
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
try {
this.finish();
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
The problem is, it does not react to touch. Could it have something to do with the way I set up refreshing in BarRefresh task?

If your BarGraph View Class is the one that you want to be clicking, you should make it HAVE an OnClickListener, instead of BEING an OnClickListener.
Here's what you do
1 remove the implements OnClickListener()
2 do something in your Activity like
bargraph.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//do whatever
}});
or you can put that code chunk inside the constructor (this.setOnClickListener()) so that all your BarGraph classes get it etc.

Related

How to fill the color gradually on drawn view canvas Android

I am having a WaveFormView on which I want to change the color of it while playing Audio file and as the Audio is paused it should be stopped coloring at that certain point and when resumed it should continue forward with coloring. I am not getting how to do it in my code..
This is the screen shot of my generated waveform. Now when I will click on Play button it should change the color of waveform gradually with red color (from start to end slowly).
Here is my code to draw waveform view.
WaveFormView.class
public class WaveformView extends View {
public interface WaveformListener {
public void waveformFling(float x);
public void waveformDraw();
}
;
// Colors
private Paint mGridPaint;
private Paint mSelectedLinePaint;
private Paint mUnselectedLinePaint;
private Paint mUnselectedBkgndLinePaint;
private Paint mBorderLinePaint;
private Paint mPlaybackLinePaint;
private Paint mTimecodePaint;
private SoundFile mSoundFile;
private int[] mLenByZoomLevel;
private double[][] mValuesByZoomLevel;
private double[] mZoomFactorByZoomLevel;
private int[] mHeightsAtThisZoomLevel;
private int mZoomLevel;
private int mNumZoomLevels;
private int mSampleRate;
private int mSamplesPerFrame;
private int mOffset;
private int mSelectionStart;
private int mSelectionEnd;
private int mPlaybackPos;
private float mDensity;
private float mInitialScaleSpan;
private WaveformListener mListener;
private GestureDetector mGestureDetector;
private ScaleGestureDetector mScaleGestureDetector;
private boolean mInitialized;
Color color;
public WaveformView(Context context, AttributeSet attrs) {
super(context, attrs);
// We don't want keys, the markers get these
setFocusable(false);
mGridPaint = new Paint();
mGridPaint.setAntiAlias(false);
mGridPaint.setColor(
getResources().getColor(R.color.grid_line));
mSelectedLinePaint = new Paint();
mSelectedLinePaint.setAntiAlias(false);
mSelectedLinePaint.setColor(
getResources().getColor(R.color.waveform_selected));
mUnselectedLinePaint = new Paint();
mUnselectedLinePaint.setAntiAlias(false);
mUnselectedLinePaint.setColor(
getResources().getColor(R.color.waveform_unselected));
mUnselectedBkgndLinePaint = new Paint();
mUnselectedBkgndLinePaint.setAntiAlias(false);
mUnselectedBkgndLinePaint.setColor(
getResources().getColor(
R.color.selection_border));
mBorderLinePaint = new Paint();
mBorderLinePaint.setAntiAlias(true);
mBorderLinePaint.setStrokeWidth(1.5f);
mBorderLinePaint.setPathEffect(
new DashPathEffect(new float[]{3.0f, 2.0f}, 0.0f));
mBorderLinePaint.setColor(
getResources().getColor(R.color.selection_border));
mPlaybackLinePaint = new Paint();
mPlaybackLinePaint.setAntiAlias(false);
mPlaybackLinePaint.setColor(
getResources().getColor(R.color.playback_indicator));
mTimecodePaint = new Paint();
mTimecodePaint.setTextSize(12);
mTimecodePaint.setAntiAlias(true);
mTimecodePaint.setColor(
getResources().getColor(R.color.timecode));
mTimecodePaint.setShadowLayer(
2, 1, 1,
getResources().getColor(R.color.timecode_shadow));
mGestureDetector = new GestureDetector(
context,
new GestureDetector.SimpleOnGestureListener() {
public boolean onFling(
MotionEvent e1, MotionEvent e2, float vx, float vy) {
mListener.waveformFling(vx);
return true;
}
});
mSoundFile = null;
mLenByZoomLevel = null;
mValuesByZoomLevel = null;
mHeightsAtThisZoomLevel = null;
mOffset = 0;
mPlaybackPos = -1;
mSelectionStart = 0;
mSelectionEnd = 0;
mDensity = 1.0f;
mInitialized = false;
}
public boolean hasSoundFile() {
return mSoundFile != null;
}
public void setSoundFile(SoundFile soundFile) {
mSoundFile = soundFile;
mSampleRate = mSoundFile.getSampleRate();
mSamplesPerFrame = mSoundFile.getSamplesPerFrame();
computeDoublesForAllZoomLevels();
mHeightsAtThisZoomLevel = null;
}
/**
* Called once when a new sound file is added
*/
private void computeDoublesForAllZoomLevels() {
int numFrames = mSoundFile.getNumFrames();
int[] frameGains = mSoundFile.getFrameGains();
double[] smoothedGains = new double[numFrames];
if (numFrames == 1) {
smoothedGains[0] = frameGains[0];
} else if (numFrames == 2) {
smoothedGains[0] = frameGains[0];
smoothedGains[1] = frameGains[1];
} else if (numFrames > 2) {
smoothedGains[0] = (double)(
(frameGains[0] / 2.0) +
(frameGains[1] / 2.0));
for (int i = 1; i < numFrames - 1; i++) {
smoothedGains[i] = (double)(
(frameGains[i - 1] / 3.0) +
(frameGains[i ] / 3.0) +
(frameGains[i + 1] / 3.0));
}
smoothedGains[numFrames - 1] = (double)(
(frameGains[numFrames - 2] / 2.0) +
(frameGains[numFrames - 1] / 2.0));
}
// Make sure the range is no more than 0 - 255
double maxGain = 1.0;
for (int i = 0; i < numFrames; i++) {
if (smoothedGains[i] > maxGain) {
maxGain = smoothedGains[i];
}
}
double scaleFactor = 1.0;
if (maxGain > 255.0) {
scaleFactor = 255 / maxGain;
}
// Build histogram of 256 bins and figure out the new scaled max
maxGain = 0;
int gainHist[] = new int[256];
for (int i = 0; i < numFrames; i++) {
int smoothedGain = (int)(smoothedGains[i] * scaleFactor);
if (smoothedGain < 0)
smoothedGain = 0;
if (smoothedGain > 255)
smoothedGain = 255;
if (smoothedGain > maxGain)
maxGain = smoothedGain;
gainHist[smoothedGain]++;
}
// Re-calibrate the min to be 5%
double minGain = 0;
int sum = 0;
while (minGain < 255 && sum < numFrames / 20) {
sum += gainHist[(int)minGain];
minGain++;
}
// Re-calibrate the max to be 99%
sum = 0;
while (maxGain > 2 && sum < numFrames / 100) {
sum += gainHist[(int)maxGain];
maxGain--;
}
// Compute the heights
double[] heights = new double[numFrames];
double range = maxGain - minGain;
for (int i = 0; i < numFrames; i++) {
double value = (smoothedGains[i] * scaleFactor - minGain) / range;
if (value < 0.0)
value = 0.0;
if (value > 1.0)
value = 1.0;
heights[i] = value * value;
}
mNumZoomLevels = 5;
mLenByZoomLevel = new int[5];
mZoomFactorByZoomLevel = new double[5];
mValuesByZoomLevel = new double[5][];
// Level 0 is doubled, with interpolated values
mLenByZoomLevel[0] = numFrames * 2;
mZoomFactorByZoomLevel[0] = 2.0;
mValuesByZoomLevel[0] = new double[mLenByZoomLevel[0]];
if (numFrames > 0) {
mValuesByZoomLevel[0][0] = 0.5 * heights[0];
mValuesByZoomLevel[0][1] = heights[0];
}
for (int i = 1; i < numFrames; i++) {
mValuesByZoomLevel[0][2 * i] = 0.5 * (heights[i - 1] + heights[i]);
mValuesByZoomLevel[0][2 * i + 1] = heights[i];
}
// Level 1 is normal
mLenByZoomLevel[1] = numFrames;
mValuesByZoomLevel[1] = new double[mLenByZoomLevel[1]];
mZoomFactorByZoomLevel[1] = 1.0;
for (int i = 0; i < mLenByZoomLevel[1]; i++) {
mValuesByZoomLevel[1][i] = heights[i];
}
// 3 more levels are each halved
for (int j = 2; j < 5; j++) {
mLenByZoomLevel[j] = mLenByZoomLevel[j - 1] / 2;
mValuesByZoomLevel[j] = new double[mLenByZoomLevel[j]];
mZoomFactorByZoomLevel[j] = mZoomFactorByZoomLevel[j - 1] / 2.0;
for (int i = 0; i < mLenByZoomLevel[j]; i++) {
mValuesByZoomLevel[j][i] =
0.5 * (mValuesByZoomLevel[j - 1][2 * i] +
mValuesByZoomLevel[j - 1][2 * i + 1]);
}
}
if (numFrames > 5000) {
mZoomLevel = 3;
} else if (numFrames > 1000) {
mZoomLevel = 2;
} else if (numFrames > 300) {
mZoomLevel = 1;
} else {
mZoomLevel = 0;
}
mInitialized = true;
}
public boolean canZoomIn() {
return (mZoomLevel > 0);
}
public void zoomIn() {
if (canZoomIn()) {
mZoomLevel--;
mSelectionStart *= 2;
mSelectionEnd *= 2;
mHeightsAtThisZoomLevel = null;
int offsetCenter = mOffset + getMeasuredWidth() / 2;
offsetCenter *= 2;
mOffset = offsetCenter - getMeasuredWidth() / 2;
if (mOffset < 0)
mOffset = 0;
invalidate();
}
}
public boolean canZoomOut() {
return (mZoomLevel < mNumZoomLevels - 1);
}
public void zoomOut() {
if (canZoomOut()) {
mZoomLevel++;
mSelectionStart /= 2;
mSelectionEnd /= 2;
int offsetCenter = mOffset + getMeasuredWidth() / 2;
offsetCenter /= 2;
mOffset = offsetCenter - getMeasuredWidth() / 2;
if (mOffset < 0)
mOffset = 0;
mHeightsAtThisZoomLevel = null;
invalidate();
}
}
public double pixelsToSeconds(int pixels) {
double z = mZoomFactorByZoomLevel[mZoomLevel];
return (pixels * (double)mSamplesPerFrame / (mSampleRate * z));
}
public void setListener(WaveformListener listener) {
mListener = listener;
}
public void recomputeHeights(float density) {
mHeightsAtThisZoomLevel = null;
mDensity = density;
mTimecodePaint.setTextSize((int)(12 * density));
invalidate();
}
protected void drawWaveformLine(Canvas canvas,
int x, int y0, int y1,
Paint paint) {
canvas.drawLine(x, y0, x, y1, paint);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (mSoundFile == null)
return;
if (mHeightsAtThisZoomLevel == null)
computeIntsForThisZoomLevel();
DisplayMetrics displaymetrics = getContext().getResources().getDisplayMetrics();
int height = displaymetrics.heightPixels;
int widths = displaymetrics.widthPixels;
// Draw waveform
int measuredWidth = getMeasuredWidth();
int measuredHeight = getMeasuredHeight();
int start = mOffset;
int width = mHeightsAtThisZoomLevel.length - start;
int ctr = measuredHeight / 2;
Log.e("wid",String.valueOf(width));
Log.e("widCal",String.valueOf(mHeightsAtThisZoomLevel.length));
Log.e("widstart",String.valueOf(start));
if (width > measuredWidth)
width = measuredWidth;
Log.e("measured",String.valueOf(measuredWidth));
// Draw grid
double onePixelInSecs = pixelsToSeconds(1);
boolean onlyEveryFiveSecs = (onePixelInSecs > 1.0 / 50.0);
double fractionalSecs = mOffset * onePixelInSecs;
int integerSecs = (int) fractionalSecs;
int i = 0;
while (i < width) {
i++;
fractionalSecs += onePixelInSecs;
int integerSecsNew = (int) fractionalSecs;
if (integerSecsNew != integerSecs) {
integerSecs = integerSecsNew;
if (!onlyEveryFiveSecs || 0 == (integerSecs % 5)) {
canvas.drawLine(i, 0, i, measuredHeight, mGridPaint);
}
}
}
// Draw waveform
for ( i = 0; i < width; i++) {
Paint paint;
if (i + start >= mSelectionStart &&
i + start < mSelectionEnd) {
paint = mSelectedLinePaint;
// paint.setColor(color);
} else {
drawWaveformLine(canvas, ((widths/width)*i), 0, measuredHeight,
mUnselectedBkgndLinePaint);
paint = mUnselectedLinePaint;
}
drawWaveformLine(
canvas, ((widths/width)*i),
ctr - mHeightsAtThisZoomLevel[start + i],
ctr + 1 + mHeightsAtThisZoomLevel[start + i],
paint);
if (i + start == mPlaybackPos) {
canvas.drawLine(i, 0, i, measuredHeight, mPlaybackLinePaint);
}
}
if (mListener != null) {
mListener.waveformDraw();
}
}
private void computeIntsForThisZoomLevel() {
int halfHeight = (getMeasuredHeight() / 2) - 1;
mHeightsAtThisZoomLevel = new int[mLenByZoomLevel[mZoomLevel]];
for (int i = 0; i < mLenByZoomLevel[mZoomLevel]; i++) {
mHeightsAtThisZoomLevel[i] =
(int)(mValuesByZoomLevel[mZoomLevel][i] * halfHeight);
}
}
}
MainActivity.class
public class MainActivity extends AppCompatActivity implements WaveformView.WaveformListener {
WaveformView mWaveformView;
SoundFile mSoundFile;
private float mDensity;
private File mFile;
private String mFilename;
private long mLoadingLastUpdateTime;
boolean mLoadingKeepGoing;
boolean mFinishActivity;
private ProgressDialog mProgressDialog;
String mTitle,mArtist;
private Thread mLoadSoundFileThread;
private Thread mRecordAudioThread;
private Thread mSaveSoundFileThread;
private boolean mIsPlaying;
private SamplePlayer mPlayer;
private String mInfoContent;
private int mWidth;
private int mMaxPos;
private int mStartPos;
private int mEndPos;
private boolean mStartVisible;
private boolean mEndVisible;
private int mLastDisplayedStartPos;
private int mLastDisplayedEndPos;
private int mOffset;
private int mOffsetGoal;
private int mFlingVelocity;
private int mPlayStartMsec;
private int mPlayEndMsec;
private Handler mHandler;
Button pla;
MediaPlayer mediaPlayer;
boolean ismIsPlaying;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pla = (Button)findViewById(R.id.play);
mWaveformView = (WaveformView)findViewById(R.id.waveform);
mWaveformView.setListener(this);
mHandler = new Handler();
Uri uri = Uri.parse("/sdcard/audio_file.mp3");
mediaPlayer = new MediaPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(),uri);
loadGui();
loadFromFile();
}
/**
* Called from both onCreate and onConfigurationChanged
* (if the user switched layouts)
*/
private void loadGui() {
// Inflate our UI from its XML layout description.
setContentView(R.layout.activity_main);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
mDensity = metrics.density;
mWaveformView = (WaveformView)findViewById(R.id.waveform);
mWaveformView.setListener(this);
if (mSoundFile != null && !mWaveformView.hasSoundFile()) {
mWaveformView.setSoundFile(mSoundFile);
mWaveformView.recomputeHeights(mDensity);
}
}
private void loadFromFile() {
mFilename = "/sdcard/audio_file.mp3";
mFile = new File(mFilename);
SongMetadataReader metadataReader = new SongMetadataReader(
this, mFilename);
mTitle = metadataReader.mTitle;
mArtist = metadataReader.mArtist;
String titleLabel = mTitle;
if (mArtist != null && mArtist.length() > 0) {
titleLabel += " - " + mArtist;
}
setTitle(titleLabel);
mLoadingLastUpdateTime = getCurrentTime();
mLoadingKeepGoing = true;
mFinishActivity = false;
mProgressDialog = new ProgressDialog(MainActivity.this);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setTitle("Loading...");
mProgressDialog.setCancelable(true);
mProgressDialog.setOnCancelListener(
new DialogInterface.OnCancelListener() {
public void onCancel(DialogInterface dialog) {
mLoadingKeepGoing = false;
mFinishActivity = true;
}
});
mProgressDialog.show();
final SoundFile.ProgressListener listener =
new SoundFile.ProgressListener() {
public boolean reportProgress(double fractionComplete) {
long now = getCurrentTime();
if (now - mLoadingLastUpdateTime > 100) {
mProgressDialog.setProgress(
(int) (mProgressDialog.getMax() * fractionComplete));
mLoadingLastUpdateTime = now;
}
return mLoadingKeepGoing;
}
};
// Load the sound file in a background thread
mLoadSoundFileThread = new Thread() {
public void run() {
try {
mSoundFile = SoundFile.create(mFile.getAbsolutePath(), listener);
if (mSoundFile == null) {
mProgressDialog.dismiss();
String name = mFile.getName().toLowerCase();
String[] components = name.split("\\.");
String err;
if (components.length < 2) {
err = getResources().getString(
R.string.no_extension_error);
} else {
err = getResources().getString(
R.string.bad_extension_error) + " " +
components[components.length - 1];
}
final String finalErr = err;
Runnable runnable = new Runnable() {
public void run() {
showFinalAlert(new Exception(), finalErr);
}
};
mHandler.post(runnable);
return;
}
mPlayer = new SamplePlayer(mSoundFile);
} catch (final Exception e) {
mProgressDialog.dismiss();
e.printStackTrace();
mInfoContent = e.toString();
runOnUiThread(new Runnable() {
public void run() {
}
});
Runnable runnable = new Runnable() {
public void run() {
showFinalAlert(e, getResources().getText(R.string.read_error));
}
};
mHandler.post(runnable);
return;
}
mProgressDialog.dismiss();
if (mLoadingKeepGoing) {
Runnable runnable = new Runnable() {
public void run() {
finishOpeningSoundFile();
}
};
mHandler.post(runnable);
} else if (mFinishActivity){
MainActivity.this.finish();
}
}
};
mLoadSoundFileThread.start();
}
private void finishOpeningSoundFile() {
mWaveformView.setSoundFile(mSoundFile);
mWaveformView.recomputeHeights(mDensity);
Log.e("sound file",mFilename);
Log.e("sound", String.valueOf(mSoundFile));
}
/**
* Show a "final" alert dialog that will exit the activity
* after the user clicks on the OK button. If an exception
* is passed, it's assumed to be an error condition, and the
* dialog is presented as an error, and the stack trace is
* logged. If there's no exception, it's a success message.
*/
private void showFinalAlert(Exception e, CharSequence message) {
CharSequence title;
if (e != null) {
Log.e("Ringdroid", "Error: " + message);
Log.e("Ringdroid", getStackTrace(e));
title = getResources().getText(R.string.alert_title_failure);
setResult(RESULT_CANCELED, new Intent());
} else {
Log.v("Ringdroid", "Success: " + message);
title = getResources().getText(R.string.alert_title_success);
}
new AlertDialog.Builder(MainActivity.this)
.setTitle(title)
.setMessage(message)
.setPositiveButton(
R.string.alert_ok_button,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
finish();
}
})
.setCancelable(false)
.show();
}
private void showFinalAlert(Exception e, int messageResourceId) {
showFinalAlert(e, getResources().getText(messageResourceId));
}
#Override
public void waveformTouchStart(float x) {
}
#Override
public void waveformTouchMove(float x) {
}
#Override
public void waveformTouchEnd() {
}
#Override
public void waveformFling(float x) {
}
#Override
public void waveformDraw() {
mWidth = mWaveformView.getMeasuredWidth();
if (mOffsetGoal != mOffset) {
// updateDisplay();
}
else if (mIsPlaying) {
// updateDisplay();
} else if (mFlingVelocity != 0) {
// updateDisplay();
}
}
private long getCurrentTime() {
return System.nanoTime() / 1000000;
}
private String getStackTrace(Exception e) {
StringWriter writer = new StringWriter();
e.printStackTrace(new PrintWriter(writer));
return writer.toString();
}
public void buttonClick(View view) {
Toast.makeText(MainActivity.this, "test", Toast.LENGTH_SHORT).show();
mediaPlayer.start();
ismIsPlaying = true;
}
}
In your onDraw(Canvas canvas) add the following lines
if (i + start <= mPlaybackPos) {
Paint mPaint = new Paint(paint);
mPaint.setColor(Color.RED);
drawWaveformLine(
canvas, ((widths/width)*i),
ctr - mHeightsAtThisZoomLevel[start + i],
ctr + 1 + mHeightsAtThisZoomLevel[start + i],
mPaint);
}
add them above the line:
if (i + start == mPlaybackPos) {
And if this works, consider to allocate the Paint-object outside the onDraw() method.

How to convert Canvas to a bitmap image in android?

I know this question has been asked before but i am unable to find any solution of my problem.
I tried using the code to create a custom view ,And i want to create an image from my canvas .
So far i am successfully creating an image, but when it creates an image it appears white screen only now comes in JPEG format ,as well as in .PNG format.
My Graphiew class is creating a graph and it does a heavy operation on onDraw.
Any suggestion appreciated . Thanks in advance
public class GraphView extends View
{
Rect r1;
Rect r2;
Rect r3;
static Bitmap bitmap;
int pixelspercm;
Paint mPaint;
private boolean mDoPaint;
public GraphView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public GraphView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
init();
}
public GraphView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// TODO Auto-generated constructor stub
}
public void init()
{
mPaint=new Paint();
int density=getResources().getDisplayMetrics().densityDpi; // for mdpi 160 px
pixelspercm=(int) (density/2.54f+0.5f);
setMinimumWidth(25 * pixelspercm);// 1575
}
#SuppressLint("DrawAllocation")
#Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
// Find out the pixel density
final float widthScale = pixelspercm / (float)100; // 100 ... 0.63
// samples per cm
final float heightScale = (float)328 / pixelspercm; // pixels 328/63=5.2
mPaint.setStrokeWidth(1);
mPaint.setColor(Color.TRANSPARENT);
mPaint.setStyle(Paint.Style.FILL);
if(null==r1)
{
r1=new Rect(0,0,getWidth(),getHeight());
}
if(null==r2)
{
r2=new Rect(0,0,getWidth(),getHeight());
}
if(null==r3)
{
r3=new Rect(0, 0,getWidth(),getHeight());
}
if(bitmap==null)
{
bitmap=Bitmap.createBitmap(getWidth(),getHeight(),Bitmap.Config.ALPHA_8);
}
if (null != bitmap && mDoPaint) {// mDoPaint=false
//canvas.drawBitmap(bitmap, r1, r2, mPaint);// no execution
return;
}
Paint paint=new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.TRANSPARENT);
// canvas.drawRect(r3,mPaint);
Paint p=new Paint();
p.setStyle(Paint.Style.FILL);
p.setColor(Color.RED);
for(float i=pixelspercm/(float)10;i<getWidth();i=i+pixelspercm/(float) 10)
{
canvas.drawLine(i,0,i,getHeight(),p);
}
for(float i=pixelspercm/(float)10;i<getHeight();i=i+pixelspercm/(float) 10)
{
canvas.drawLine(0,i,getWidth(),i,p);
}
for(int i=pixelspercm;i<getHeight();i=i+pixelspercm)
{
p.setStrokeWidth(2);
canvas.drawLine(0,i,getWidth(),i, p);
}
int count=0;
int y=0;
// draw 1 cm line for 10 mm/mv
for(int i=pixelspercm;i<getWidth();i=i+pixelspercm)
{
p.setStrokeWidth(2);
canvas.drawLine(i,0,i,getHeight(),p);
for (int k = 0; k < 12; k++) {
y = ((k * 4) * pixelspercm) + 15; //15,204,393
canvas.drawText(count + "", i + 2, y,p);// display 1numbers 1 to 24
}
count++;
}
int mP1 = 0, mPp1 = 0;
int mP2 = 0, mPp2 = 0;
int mP3 = 0, mPp3 = 0;
int mP4 = 0, mPp4 = 0;
int mP5 = 0, mPp5 = 0;
int mP6 = 0, mPp6 = 0;
int mP7 = 0, mPp7 = 0;
int mP8 = 0, mPp8 = 0;
int mP9 = 0, mPp9 = 0;
int mP10 = 0, mPp10 = 0;
int mP11 = 0, mPp11 = 0;
int mP12 = 0, mPp12 = 0;
float mX = mP1 = mP2 = mP3 = 0;
mP4 = mP5 = mP6 = mP7 = mP8 = mP9 = mP10 = mP11 = mP12 = 0;
final int graphHeight = pixelspercm;
float mPx1 = 0;
mPp1 = 2 * graphHeight; // 126
float mPx2 = 0;
mPp2 = 5 * graphHeight; // graphHeight * 2; 315
float mPx3 = 0;
mPp3 = 8 * graphHeight; // graphHeight * 3; 504
float mPx4 = 0;
mPp4 = 11 * graphHeight; // graphHeight * 3; 693
float mPx5 = 0;
mPp5 = 14 * graphHeight; // graphHeight * 3;
float mPx6 = 0;
mPp6 = 17 * graphHeight; // graphHeight * 3;
float mPx7 = 0;
mPp7 = 20 * graphHeight; // graphHeight * 3;
float mPx8 = 0;
mPp8 = 23 * graphHeight; // graphHeight * 3;
float mPx9 = 0;
mPp9 = 26 * graphHeight; // graphHeight * 3;
float mPx10 = 0;
mPp10 = 29 * graphHeight; // graphHeight * 3;
float mPx11 = 0;
mPp11 = 32 * graphHeight; // graphHeight * 3;
float mPx12 = 0;
mPp12 = 35 * graphHeight; // graphHeight * 3;
final int noOfLeads =12;
final int leadSampleCount = 2500;
// traverse through the points one by one to get moving graphs
for (int i = 0; i < PloxXYarray.ecgdata2.length; i++) { // 2500 times
if (mX > getWidth()) {
break;
}
mX += widthScale;
mPaint.setColor(Color.BLACK);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(2);
// draw ECG
if (noOfLeads >= 1) {
mP1 = (2 * graphHeight)- (int) ( PloxXYarray.ecgdata1[i] / heightScale);
canvas.drawLine(mPx1, mPp1, mX, mP1,mPaint);
}
// store current point as previous point
mPp1 = mP1;
mPp2 = mP2;
mPp3 = mP3;
mPp4 = mP4;
mPp5 = mP5;
mPp6 = mP6;
mPp7 = mP7;
mPp8 = mP8;
mPp9 = mP9;
mPp10 = mP10;
mPp11 = mP11;
mPp12 = mP12;
mPx1 = mPx2 = mPx3 = mPx4 = mPx5 = mPx6 = mPx7 = mPx8 = mPx9 = mPx10 = mPx11 = mPx12 = mX;
}
canvas.drawBitmap(bitmap,r1,r2,paint);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
WindowManager wm = (WindowManager) this.getContext().getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
setMeasuredDimension(25*pixelspercm,48*pixelspercm);
}
And this is the code for my MainActivity
public class GraphViewActivity extends Activity{
GraphView graphView;
Button mSave;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.graphview);
graphView=(GraphView) findViewById(R.id.graph);
mSave=(Button) findViewById(R.id.btnSave);
final Paint mPaint=new Paint();
mPaint.setStrokeWidth(1);
mPaint.setColor(Color.WHITE);
mPaint.setStyle(Paint.Style.FILL);
mSave.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Bitmap saveMbitmap = Bitmap.createBitmap(GraphView.bitmap.getWidth(),GraphView.bitmap.getWidth(), Config.ARGB_8888);
Canvas canvas=new Canvas(saveMbitmap);
canvas.drawRect(new Rect(0,0,GraphView.bitmap.getWidth(),GraphView.bitmap.getHeight()), mPaint);
canvas.drawBitmap(GraphView.bitmap, new Rect(0,0,GraphView.bitmap.getWidth(),GraphView.bitmap.getHeight()), new Rect(0,0,GraphView.bitmap.getWidth(),GraphView.bitmap.getHeight()), null);
try {
FileOutputStream fos=new FileOutputStream(new File(Environment
.getExternalStorageDirectory().toString(), "SCREEN_Ecg_MP"+System.currentTimeMillis()+ ".jpeg"));
saveMbitmap.compress(Bitmap.CompressFormat.JPEG,100,fos);
} catch (Exception e) {
Log.e("Error--------->", e.toString());
}
// canvas.drawBitmap(GraphView.bitmap,r1,r2,mPaint);
}
});
}

Android game in surfaceview lagg spikes

guys. I'm playing around with making my very first Android game, but stumbled into a problem. The framerate seems to have random lag spikes. If I comment the background(s) out the framerate gets much smoother. I've looked around SO and can't find anything to solve my problems. I have a feeling it has something to do with allocating a specific amount of time every time I draw, but I don't know how to properly implement such a feature. Any suggestions? Btw, tryed hardware ac, anti etc.
This is the class that starts the surfaceview :
package com.example.glassrunner;
Imports Here
public class Game extends Activity
{
MySurfaceView mySurfaceView;
public SoundPool spool;
private int soundID;
int length=0;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
mySurfaceView = new MySurfaceView(this);
setContentView(mySurfaceView);
}
#Override
protected void onResume()
{
// TODO Auto-generated method stub
super.onResume();
mySurfaceView.onResumeMySurfaceView();
}
#Override
protected void onPause()
{
// TODO Auto-generated method stub
super.onPause();
mySurfaceView.onPauseMySurfaceView();
}
#Override
protected void onDestroy()
{
super.onDestroy();
mySurfaceView = null;
}
}
This is the surfaceview class :
package com.example.glassrunner;
Imports here
public class MySurfaceView extends SurfaceView implements Runnable
{
public static boolean gameOver = false;
SurfaceHolder surfaceHolder;
Thread thread = null;
public Integer score=0;
public SoundPool spool;
private int soundID;
int length=0;
public static MediaPlayer mp;
volatile boolean running = false;
int Yposition = 450;
int Xposition = 50;
Paint textPaint;
long mLastTime;
Bitmap background;
Bitmap background2;
Bitmap lines;
Bitmap runSprite;
Bitmap box;
Paint bitmapPaint ;
Paint textPaint2;
Bitmap scaledBackground ;
Bitmap scaledBackground2 ;
Bitmap scaledLines ;
Bitmap scaledBox;
Canvas canvas;
Paint paint;
int SpX=0;
int SpY=0;
Bitmap[][] sprite;
/** Variables for the counter */
int frameSamplesCollected = 0;
int frameSampleTime = 0;
int fps = 0;
int speed = 5;
Toast GameOverToast;
Context context;
MediaPlayer mMediaPlayer;
public MySurfaceView(Context context)
{
super(context);
this.context = context;
// TODO Auto-generated constructor stub
surfaceHolder = getHolder();
surfaceHolder.setFormat(PixelFormat.RGB_565);
CharSequence text = "Game Over!";
int duration = Toast.LENGTH_SHORT;
GameOverToast = Toast.makeText(context, text, duration);
spool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
soundID = spool.load(context, R.raw.jump, 1);
mp = MediaPlayer.create(context, R.raw.saturdaymorningfunk);
initialization();
}
public void initialization()
{
mp.setLooping(true);
mp.start();
Options options = new Options();
options.inSampleSize = 1/4;
options.inPreferredConfig = Bitmap.Config.RGB_565;
background=BitmapFactory.decodeResource(getResources(),R.drawable.background,options);
lines=BitmapFactory.decodeResource(getResources(),R.drawable.lines);// getting the png from drawable folder
background2=BitmapFactory.decodeResource(getResources(),R.drawable.background2,options);
runSprite=BitmapFactory.decodeResource(getResources(),R.drawable.runsprite);
box=BitmapFactory.decodeResource(getResources(),R.drawable.box);
bitmapPaint = new Paint(Paint.ANTI_ALIAS_FLAG); // tool for painting on the canvas
bitmapPaint.setAntiAlias(true);
bitmapPaint.setFilterBitmap(true);
textPaint = new Paint();
textPaint.setColor(Color.RED);
textPaint.setTextSize(32);
textPaint2 = new Paint();
textPaint2.setColor(Color.BLUE);
textPaint2.setTextSize(50);
scaledBackground = Bitmap.createScaledBitmap(background, 2560, 500, true);
scaledBackground2 = Bitmap.createScaledBitmap(background2, 2560, 400, true);
scaledLines = Bitmap.createScaledBitmap(lines, 2560, 30, true);
runSprite = Bitmap.createScaledBitmap(runSprite, 1400, 1000, true);
scaledBox = Bitmap.createScaledBitmap(box, 100, 100, true);
sprite = new Bitmap[4][7];
for(int row=0;row<=3;row++)
{
for(int col=0;col<=6;col++)
{
sprite[row][col] = Bitmap.createBitmap(runSprite, SpX, SpY, 200, 250);
SpX+=200;
}
SpX=0;
SpY+=250;
}
}
public void onResumeMySurfaceView()
{
mp.seekTo(length);
mp.start();
running = true;
thread = new Thread(this);
thread.start();
}
public void onPauseMySurfaceView()
{
mp.pause();
length=mp.getCurrentPosition();
boolean retry = true;
running = false;
while(retry){
try {
thread.join();
retry = false;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void onDestroyMySurfaceView()
{
mp.stop();
running = false;
thread = null;
thread.stop();
}
private void fps()
{
long now = System.currentTimeMillis();
if (mLastTime != 0)
{
//Time difference between now and last time we were here
int time = (int) (now - mLastTime);
frameSampleTime += time;
frameSamplesCollected++;
//After 10 frames
if (frameSamplesCollected == 10)
{
//Update the fps variable
fps = (int) (10000 / frameSampleTime);
//Reset the sampletime + frames collected
frameSampleTime = 0;
frameSamplesCollected = 0;
}
}
mLastTime = now;
}
public boolean pressDown = false;
public long pressTime;
public boolean onTouchEvent(MotionEvent event)
{
if (event != null)
{
if (event.getAction() == MotionEvent.ACTION_DOWN)
{ if(Yposition == orgPos)
{
spool.play(soundID, 15, 15, 1, 0, 1f);
pressDown = true;
pressTime = System.currentTimeMillis();
}
}else if (event.getAction() == MotionEvent.ACTION_UP)
{
pressDown = false;
}
}
return true;
}
int x=0;
int y=100;
int x2=0;
int y2=20;
int row=0;
int col=0;
int limit = 100;
int orgPos = 450;
int Xbox = 1280;
int Ybox = 580;
Random r = new Random();
int RBox;
public static String Fscore;
boolean onTop = false;
long now;
long start;
long stop;
long time ;
int spritePosition = 0 ;
int spriteSize;
#Override
public void run()
{
while(running)
{
canvas = null;
if(surfaceHolder.getSurface().isValid())
{
canvas = surfaceHolder.lockCanvas();
fps(); // fps
// Update screen parameters
update();
draw();
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
public void update()
{
if(score<500)
{
speed = 7;
}
else if(score%500 == 0)
{
speed = 7 + (score / 500);
}
if(col==6)
{
row++;
col=0;
}
if(row==4)
{
row=0;
}
score++;
Fscore = score.toString();
if(x>-1280)
{
x-=speed;
}else if(x<=-1280)
{
x=0;
}
if(x2>-1280)
{
x2-=5;
}else if(x2<=-1280)
{
x2=-0;
}
RBox = r.nextInt(999)+1280;
if(Xbox > -100)
{
Xbox-=speed;
}else if(Xbox<=-100)
{
Xbox=RBox;
}
if( (Xposition + 200 == Xbox +40 )&&(Yposition + 250 > Ybox+20)||( Xposition+200<=Xbox+70)&&( Xposition+200>=Xbox+20)&&(Yposition + 250 > Ybox+30) ) // collision
{
GameOverToast.show();
running = false;
spool.release();
mp.release();
Looper.prepare();
Intent database = new Intent(context, MainHighscore.class);
database.putExtra("score", Fscore);
context.startActivity(database);
onDestroyMySurfaceView();
}
now = System.currentTimeMillis();
if(( now - pressTime) <= 600)
{
if(Yposition > limit)
{
Yposition -= 10;
}
}
onTop = false;
if((now - pressTime) >= 600 && (now - pressTime) <= 1200)
{
if(!(Yposition == orgPos))
{
if(Yposition+250 >= Ybox && Xposition+200>=Xbox+70 && Xposition <= Xbox+40)
{
onTop=true;
Yposition = 340;
}else
{
Yposition += 10;
}
}
}
if((now - pressTime) >= 1200)
{
if(Yposition < 450) Yposition +=10;
else Yposition = 450;
}
}
public void draw()
{
canvas.drawColor(Color.WHITE);
//canvas.drawBitmap(scaledBackground, x2,y2, bitmapPaint);
canvas.drawBitmap(scaledBackground2, x,y, bitmapPaint);
canvas.drawBitmap(scaledLines, x,650, bitmapPaint);
canvas.drawText(Fscore, 1050, 50, textPaint2);
canvas.drawText(fps + " fps", getWidth() / 2, getHeight() / 2, textPaint);
canvas.drawBitmap(sprite[row][col],Xposition,Yposition,bitmapPaint );
canvas.drawBitmap(scaledBox,Xbox,Ybox,bitmapPaint);
col++;
}
}
I think your problem might be actually the moving part. Your just drawing too much stuff, and the surfaceView is not meant for that.

Ontouch() isn't work with custom view?

I have a class it extends "View" and Implement "OnTouchListener" so it's will override ontouch method automatically but this method not work. I try to debug by put breakpoint in this method but it isn't called.
I try to this problem from another forums. they tell that onTouch() must return true; But it's not better.
my class problem :
public class Viewer extends View implements OnTouchListener{
/** Constant tile width. */
public static final int TILEW = 32;
/** Constant tile height. */
public static final int TILEH = 70;
/** Constant tile shift due to increased level. */
public static final int TILESKEW = 7;
/** Tile images. */
private Bitmap[][] tileImages;
/** Highlighted tile images. */
private Bitmap[][] tileImagesHL;
/** Board itself. */
private Board b;
private Tile[][][] tTile;
private float screenWidth = Defines.getScreenWidth();
private float screenHeight = Defines.getScreenHeight();
private float offsetImg = Defines.getOffsetImage();
private float reScalePosX = Defines.getReScalePosX();
private float reScalePosY = Defines.getReScalePosY();
private boolean firstIter;
/** Stores selected tiles */
private ArrayList<Tile> selectedTiles;// = new ArrayList<Tile>();
/** Stores highlighted tiles by Hint */
private ArrayList<Tile> highlightedTiles;// = new ArrayList<Tile>();
//////////////////////////////////////////////////
private static RectF screen;
private Images imgObj;
public Viewer(Context context) {
super(context);
// TODO Auto-generated constructor stub
imgObj = new Images(context);
b = new MainActivity().board;
Defines.setScreenRender(1280, 800);
makeTileImages(Board.MAXGROUPS);
}
#Override
protected void onDraw(Canvas canvas){
drawBackGround(imgObj.bg01, canvas);
try{
tTile = b.getContent();
if(tTile==null || tileImages==null){
return;
}
drawTile(tTile, canvas);
}catch (Exception e) {
// TODO: handle exception
}
}
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
Log.d("Mydebug", "YEHHHHHH");
Tile[][][] content = b.getContent();
for (int i = 0; i < content.length; i++) {
for (int y = 0; y < content[i].length; y++) {
for (int x = 0; x < content[i][y].length; x++) {
processMouseClick(content, i, y , x, event);
}
}
}
return true;
}
public void drawBackGround(Bitmap bitmapBG,Canvas canvas){
screen = new RectF(0.0f, 0.0f, Defines.getScreenWidth(),Defines.getScreenHeight());
canvas.drawBitmap(bitmapBG, null, screen, null);
}
private void makeTileImages(int groups){
tileImages = loadTileset(groups, "unlit_");
tileImagesHL = loadTileset(groups, "lit_");
}
private Bitmap[][] loadTileset(int groups,String prefix){
Bitmap[][] tr = new Bitmap[groups][Board.GROUPSIZE];
try{
InputStream is = this.getResources().getAssets().open("Tiles/tiles.set");
BufferedReader f = new BufferedReader(new InputStreamReader(is));
String s = f.readLine();
while (s!=null) {
if (!s.startsWith("#")) {
String[] tokens = s.split(" ");
try{
int groupNum =Integer.parseInt(tokens[0]);
for(int i =1; i<=(Board.GROUPSIZE);i++){
String pathPNG = "Tiles/"+prefix+tokens[i]+".png";
Bitmap img = imgObj.loadImage(pathPNG);
img = imgObj.resizeBitmap(img,Tile.tileHeight, Tile.tileWidth);
//Bitmap img = loadImage(pathPNG);
//Log.d("Mydebug", "PATH:"+pathPNG);
tr[groupNum][i-1] = img;
}
}catch (Exception e) {
// TODO: handle exception
Log.d("Mydebug", "File error TIles/tile.set");
}
}
s = f.readLine();
}
}catch (Exception e) {
// TODO: handle exception
}
return tr;
}
public void drawTile(Tile[][][] content,Canvas canvas){
for (int i = 0; i < content.length; i++) {
for (int y = 0; y < content[i].length; y++) {
for (int x = 0; x < content[i][y].length; x++) {
final Tile t = content[i][y][x];
if (t != null) {
if (y>0 && content[i][y-1][x]!=null && t.equals(content[i][y-1][x])) {
continue;
}
if (x>0 && content[i][y][x-1]!=null && t.equals(content[i][y][x-1])) {
continue;
}
Bitmap image = tileImages[t.getValue()][t.getSubindex()];
if (b.free(t)) {
image = tileImagesHL[t.getValue()][t.getSubindex()];
}
/* Draw tile images to panel */
//canvas.drawBitmap(image, x*TILEW+TILEW/2+i*TILESKEW, (y+1)*TILEH/2-i*TILESKEW, null);
canvas.drawBitmap(image, (x*TILEW+TILEW/2+i*TILESKEW)+200, ((y+1)*TILEH/2-i*TILESKEW)+100, null);
}
}
}
}
}
/** A helper function for the Viewer constructor which accompanies the mouseListener object on each tiles
* #param content The current board's 3D array of positions
* #param i The current tile's x position
* #param y The current tile's y position
* #param x The current tile's z position
* #param me The mouseEvent for each tile
*/
private void processMouseClick(Tile[][][] content, int i, int y, int x, MotionEvent touchPos){
final Tile t = content[i][y][x];
if (t != null) {
if (y>0 && content[i][y-1][x]!=null && t.equals(content[i][y-1][x])) {
return;
}
if (x>0 && content[i][y][x-1]!=null && t.equals(content[i][y][x-1])) {
return;
}
Bitmap image = tileImages[t.getValue()][t.getSubindex()];
if (b.free(t)) {
image = tileImagesHL[t.getValue()][t.getSubindex()];
}
// Rectangle representing space of tile
final Rect rect = new Rect(x*TILEW+TILEW/2+i*TILESKEW, (y+1)*TILEH/2-i*TILESKEW, image.getWidth(), image.getHeight());
//if ((rect.contains(me.getPoint())) && b.free(t) && t.isVisible()) {
if ((rect.contains((int)touchPos.getX(), (int)touchPos.getY()) && b.free(t))){
t.wasClicked = true;
firstIter = false;
Log.d("MyDebug", "Clicked ME");
//if (content[i][y][x].isVisible()){
//Add corresponding JLabel to panel
// t.tLabel.setBounds(x*TILEW+TILEW/2+i*TILESKEW, (y+1)*TILEH/2-i*TILESKEW, image.getWidth(null), image.getHeight(null));
// t.tLabel.setVisible(false);
// t.tLabel.setBorder(BorderFactory.createLineBorder(Color.blue, 2));
//}
//validClick(t);
}
}
}
}
remove implements OnTouchListener and override the method:
public boolean onTouch(MotionEvent event)
update
of course it's onTouchEvent, not onTouch

Visual defects when I use drawBitmap()

It looks like this both in the emulator and on the phone. It's always a random square that is broken.
And here is the code from the SurfaceView class, the most relevant part is in prepareBackground() :
Code begins:
public class MenuBackgroundView extends SurfaceView implements SurfaceHolder.Callback
{
private Paint mPaint;
private Bitmap blocks12x12;
private boolean draw = false;
private Context ctx;
//private int begOffX;
private int offX;
private int offY;
private int mWidth = 1;
private int mHeight = 1;
private ViewThread mThread;
private int calcSizeXY;
public MenuBackgroundView(Context context, AttributeSet attrs) {
super(context, attrs);
getHolder().addCallback(this);
mThread = new ViewThread(this);
mPaint = new Paint();
mPaint.setColor(Color.RED);
ctx = context;
}
public void doDraw(long elapsed, Canvas canvas) {
canvas.drawColor(Color.BLUE);
if(draw)
{
canvas.drawBitmap(blocks12x12, offX, offY, mPaint);
}
canvas.drawText("FPS: " + Math.round(1000f / elapsed) + " Elements: ", 10, 10, mPaint);
}
public void animate(long elapsed)
{
/*
//elapsed = elapsed/10;
offX = (offX+2);
if(offX >= 0)
{
offX -= 2*calcSizeXY;
}
offY = (offY+3);
if(offY >= 0)
{
offY -= 2*calcSizeXY;
}
//offY = (offY + (int)(elapsed/10f)) % calcSizeXY*2;//
*
*///
}
public void prepareBackground()
{
if(mWidth <= 1 || mHeight <= 1 )
return;
Log.d("Menu", "prepareBackground");
if(mHeight > mWidth)
{
calcSizeXY = mHeight/10;
}
else
{
calcSizeXY = mWidth/10;
}
offX = -2*calcSizeXY;
Bitmap block = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.block);
block = Bitmap.createScaledBitmap(block, calcSizeXY, calcSizeXY, false);
// Group together
int sizeX = 12*calcSizeXY;
int sizeY = 12*calcSizeXY;
blocks12x12 = Bitmap.createBitmap(sizeX, sizeY, Bitmap.Config.ARGB_8888);
Canvas blocks12x12Canvas = new Canvas(blocks12x12);
for(int i = 0; i < 14; i+=2)
{
for(int j = 0; j < 14; j+=2)
{
blocks12x12Canvas.drawBitmap(block, (j*calcSizeXY), (i*calcSizeXY), mPaint);
}
}
// "Memory leak"
block.recycle();
draw = true;
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
mWidth = width;
mHeight = height;
prepareBackground();
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
prepareBackground();
if (!mThread.isAlive()) {
mThread = new ViewThread(this);
mThread.setRunning(true);
mThread.start();
}
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
if (mThread.isAlive()) {
mThread.setRunning(false);
freeMem();
}
}
public void freeMem() {
// TODO Auto-generated method stub
draw = false;
blocks12x12.recycle(); // "Memory leak"
}
}
Ok I figured it out, simply make the thread sleep for 1ms:
blocks12x12 = Bitmap.createBitmap(sizeX, sizeY, Bitmap.Config.ARGB_8888);
Canvas blocks12x12Canvas = new Canvas(blocks12x12);
for(int i = 0; i < 14; i+=2)
{
for(int j = 0; j < 14; j+=2)
{
try {
Thread.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
blocks12x12Canvas.drawBitmap(block, (j*calcSizeXY), (i*calcSizeXY), mPaint);
}
}

Categories

Resources