With help of some question i managed to add volume and brightness control to my video player with onTouchEvent method. But when i swipe up or down my app close(it Restarts). Any help on my code why am not able to control volume and brightness using onTouchEvent.
Here is my code:
#Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
tested_ok=false;
if (event.getX() < (sWidth / 2)) {
intLeft = true;
intRight = false;
} else if (event.getX() > (sWidth / 2)) {
intLeft = false;
intRight = true;
}
int upperLimit = (sHeight / 4) + 100;
int lowerLimit = ((sHeight / 4) * 3) - 150;
if (event.getY() < upperLimit) {
intBottom = false;
intTop = true;
} else if (event.getY() > lowerLimit) {
intBottom = true;
intTop = false;
} else {
intBottom = false;
intTop = false;
}
seekSpeed = (TimeUnit.MILLISECONDS.toSeconds(exoPlayer.getDuration()) * 0.1);
diffX = 0;
calculatedTime = 0;
seekDur = String.format("%02d:%02d", TimeUnit.MILLISECONDS.toMinutes(diffX) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(diffX)),
TimeUnit.MILLISECONDS.toSeconds(diffX) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(diffX)));
//TOUCH STARTED
baseX = event.getX();
baseY = event.getY();
break;
case MotionEvent.ACTION_MOVE:
screen_swipe_move=true;
root.setVisibility(View.GONE);
diffX = (long) (Math.ceil(event.getX() - baseX));
diffY = (long) Math.ceil(event.getY() - baseY);
double brightnessSpeed = 0.05;
if (Math.abs(diffY) > MIN_DISTANCE) {
tested_ok = true;
}
if (Math.abs(diffY) > Math.abs(diffX)) {
if (intLeft) {
cResolver = getContentResolver();
window = getWindow();
try {
Settings.System.putInt(cResolver, Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
brightness = Settings.System.getInt(cResolver, Settings.System.SCREEN_BRIGHTNESS);
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
int new_brightness = (int) (brightness - (diffY * brightnessSpeed));
if (new_brightness > 250) {
new_brightness = 250;
} else if (new_brightness < 1) {
new_brightness = 1;
}
double brightPerc = Math.ceil((((double) new_brightness / (double) 250) * (double) 100));
brightnessBarContainer.setVisibility(View.VISIBLE);
brightness_center_text.setVisibility(View.VISIBLE);
brightnessBar.setProgress((int) brightPerc);
brigtness_perc_center_text.setText(" " + (int) brightPerc);
Settings.System.putInt(cResolver, Settings.System.SCREEN_BRIGHTNESS, (new_brightness));
WindowManager.LayoutParams layoutpars = window.getAttributes();
layoutpars.screenBrightness = brightness / (float) 255;
window.setAttributes(layoutpars);
}else if (intRight) {
vol_center_text.setVisibility(View.VISIBLE);
mediavolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
int maxVol = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
double cal = (double) diffY * ((double)maxVol/(double)(device_height*4));
int newMediaVolume = mediavolume - (int) cal;
if (newMediaVolume > maxVol) {
newMediaVolume = maxVol;
} else if (newMediaVolume < 1) {
newMediaVolume = 0;
}
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, newMediaVolume, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
double volPerc = Math.ceil((((double) newMediaVolume / (double) maxVol) * (double) 100));
vol_perc_center_text.setText(" " + (int) volPerc);
volumeBarContainer.setVisibility(View.VISIBLE);
volumeBar.setProgress((int) volPerc);
}
}else if (Math.abs(diffX) > Math.abs(diffY)) {
if (Math.abs(diffX) > (MIN_DISTANCE + 100)) {
}
}
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
screen_swipe_move=false;
tested_ok = false;
seekBar_center_text.setVisibility(View.GONE);
brightness_center_text.setVisibility(View.GONE);
vol_center_text.setVisibility(View.GONE);
brightnessBarContainer.setVisibility(View.GONE);
volumeBarContainer.setVisibility(View.GONE);
onlySeekbar.setVisibility(View.VISIBLE);
root.setVisibility(View.VISIBLE);
calculatedTime = (int) (exoPlayer.getCurrentPosition() + (calculatedTime));
exoPlayer.seekTo(calculatedTime);
break;
}
return super.onTouchEvent(event);
}
FYI: i can't make Log since i don't have emulator. your help would be more helpful thanks in advance.
Edit: when i swipe down on volume it move to zero, it doesn't reduce slowly and only the volume down works.
Here is complete code of VideoPlayerActivity.java
package com.sanoj.jlplayer.Activities;
public class VideoPlayerActivity extends AppCompatActivity {
Uri videoUri;
#BindView(R.id.playerView) PlayerView playerView;
#BindView(R.id.floating_widget) ImageView imageView;
#BindView(R.id.exoFullscreenIcon) ImageView imageView1;
#BindView(R.id.vol_perc_center_text) TextView vol_perc_center_text;
#BindView(R.id.brigtness_perc_center_text) TextView brigtness_perc_center_text;
#BindView(R.id.volume_slider) ProgressBar volumeBar;
#BindView(R.id.brightness_slider) ProgressBar brightnessBar;
#BindView(R.id.volume_slider_container) LinearLayout volumeBarContainer;
#BindView(R.id.brightness_slider_container) LinearLayout brightnessBarContainer;
#BindView(R.id.brightness_center_text) LinearLayout brightness_center_text;
#BindView(R.id.vol_center_text) LinearLayout vol_center_text;
#BindView(R.id.volIcon) ImageView volIcon;
#BindView(R.id.brightnessIcon) ImageView brightnessIcon;
#BindView(R.id.vol_image) ImageView vol_image;
#BindView(R.id.brightness_image) ImageView brightness_image;
boolean fullscreen = false;
ExoPlayer exoPlayer;
ExtractorsFactory extractorsFactory;
int sWidth,sHeight;
float baseX, baseY;
long diffX, diffY;
int calculatedTime;
String seekDur;
Boolean tested_ok = false;
Boolean screen_swipe_move = false;
boolean immersiveMode, intLeft, intRight, intTop, intBottom, finLeft, finRight, finTop, finBottom;
static final int MIN_DISTANCE = 150;
ContentResolver cResolver;
Window window;
TextView txt_seek_secs,txt_seek_currTime;
int brightness, mediavolume,device_height,device_width;
AudioManager audioManager;
double seekSpeed = 0;
LinearLayout root, seekBar_center_text, onlySeekbar;
Point size;
Display display;
SeekBar seekBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_player);
ButterKnife.bind(this);
Intent intent = getIntent();
//audio and brightness
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
display = getWindowManager().getDefaultDisplay();
size = new Point();
display.getSize(size);
sWidth = size.x;
sHeight = size.y;
if (intent!=null){
String uri_str = intent.getStringExtra("videoUri");
videoUri = Uri.parse(uri_str);
}
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
exoPlayer.setPlayWhenReady(false);
exoPlayer.release();
Intent serviceIntent = new Intent(VideoPlayerActivity.this,FloatingWidgetService.class);
serviceIntent.putExtra("videoUri",videoUri.toString());
startService(serviceIntent);
}
});
BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
TrackSelector trackSelector = new DefaultTrackSelector(new AdaptiveTrackSelection.Factory(bandwidthMeter));
exoPlayer = ExoPlayerFactory.newSimpleInstance(this,trackSelector);
extractorsFactory = new DefaultExtractorsFactory();
playVideo();
}
private void playVideo() {
try {
String playerInfo = Util.getUserAgent(this,"VideoPlayer");
DefaultDataSourceFactory dataSourceFactory = new DefaultDataSourceFactory(this,playerInfo);
MediaSource mediaSource = new ExtractorMediaSource(videoUri,dataSourceFactory,extractorsFactory,null,null);
playerView.setPlayer(exoPlayer);
exoPlayer.prepare(mediaSource);
exoPlayer.setPlayWhenReady(true);
}catch (Exception e){
e.printStackTrace();
}
}
#Override
protected void onPause() {
super.onPause();
exoPlayer.setPlayWhenReady(false);
}
#Override
public void onBackPressed() {
super.onBackPressed();
exoPlayer.setPlayWhenReady(false);
exoPlayer.release();
}
#Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
tested_ok=false;
if (event.getX() < (sWidth / 2)) {
intLeft = true;
intRight = false;
} else if (event.getX() > (sWidth / 2)) {
intLeft = false;
intRight = true;
}
int upperLimit = (sHeight / 4) + 100;
int lowerLimit = ((sHeight / 4) * 3) - 150;
if (event.getY() < upperLimit) {
intBottom = false;
intTop = true;
} else if (event.getY() > lowerLimit) {
intBottom = true;
intTop = false;
} else {
intBottom = false;
intTop = false;
}
seekSpeed = (TimeUnit.MILLISECONDS.toSeconds(exoPlayer.getDuration()) * 0.1);
diffX = 0;
calculatedTime = 0;
seekDur = String.format("%02d:%02d",
TimeUnit.MILLISECONDS.toMinutes(diffX) -
TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(diffX)),
TimeUnit.MILLISECONDS.toSeconds(diffX) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(diffX)));
//TOUCH STARTED
baseX = event.getX();
baseY = event.getY();
break;
case MotionEvent.ACTION_MOVE:
screen_swipe_move=true;
root.setVisibility(View.GONE);
diffX = (long) (Math.ceil(event.getX() - baseX));
diffY = (long) Math.ceil(event.getY() - baseY);
double brightnessSpeed = 0.05;
if (Math.abs(diffY) > MIN_DISTANCE) {
tested_ok = true;
}
if (Math.abs(diffY) > Math.abs(diffX)) {
if (intLeft) {
cResolver = getContentResolver();
window = getWindow();
try {
Settings.System.putInt(cResolver, Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
brightness = Settings.System.getInt(cResolver, Settings.System.SCREEN_BRIGHTNESS);
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
int new_brightness = (int) (brightness - (diffY * brightnessSpeed));
if (new_brightness > 250) {
new_brightness = 250;
} else if (new_brightness < 1) {
new_brightness = 1;
}
double brightPerc = Math.ceil((((double) new_brightness / (double) 250) * (double) 100));
brightnessBarContainer.setVisibility(View.VISIBLE);
brightness_center_text.setVisibility(View.VISIBLE);
brightnessBar.setProgress((int) brightPerc);
if (brightPerc < 30) {
brightnessIcon.setImageResource(R.drawable.ic_bright_min);
brightness_image.setImageResource(R.drawable.ic_bright_min);
} else if (brightPerc > 30 && brightPerc < 80) {
brightnessIcon.setImageResource(R.drawable.ic_bright_med);
brightness_image.setImageResource(R.drawable.ic_bright_med);
} else if (brightPerc > 80) {
brightnessIcon.setImageResource(R.drawable.ic_bright_max);
brightness_image.setImageResource(R.drawable.ic_bright_max);
}
brigtness_perc_center_text.setText(" " + (int) brightPerc);
Settings.System.putInt(cResolver, Settings.System.SCREEN_BRIGHTNESS, (new_brightness));
WindowManager.LayoutParams layoutpars = window.getAttributes();
layoutpars.screenBrightness = brightness / (float) 255;
window.setAttributes(layoutpars);
}else if (intRight) {
vol_center_text.setVisibility(View.VISIBLE);
mediavolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
int maxVol = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
double cal = (double) diffY * ((double)maxVol/(double)(device_height*4));
int newMediaVolume = mediavolume - (int) cal;
if (newMediaVolume > maxVol) {
newMediaVolume = maxVol;
} else if (newMediaVolume < 1) {
newMediaVolume = 0;
}
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, newMediaVolume, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
double volPerc = Math.ceil((((double) newMediaVolume / (double) maxVol) * (double) 100));
vol_perc_center_text.setText(" " + (int) volPerc);
if (volPerc < 1) {
volIcon.setImageResource(R.drawable.ic_volume_mute);
vol_image.setImageResource(R.drawable.ic_volume_mute);
vol_perc_center_text.setVisibility(View.GONE);
} else if (volPerc >= 1) {
volIcon.setImageResource(R.drawable.ic_volume);
vol_image.setImageResource(R.drawable.ic_volume);
vol_perc_center_text.setVisibility(View.VISIBLE);
}
volumeBarContainer.setVisibility(View.VISIBLE);
volumeBar.setProgress((int) volPerc);
}
}else if (Math.abs(diffX) > Math.abs(diffY)) {
if (Math.abs(diffX) > (MIN_DISTANCE + 100)) {
tested_ok = true;
root.setVisibility(View.VISIBLE);
seekBar_center_text.setVisibility(View.VISIBLE);
onlySeekbar.setVisibility(View.VISIBLE);
(exoPlayer.getCurrentPosition() + (calculatedTime)));
}
}
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
screen_swipe_move=false;
tested_ok = false;
seekBar_center_text.setVisibility(View.GONE);
brightness_center_text.setVisibility(View.GONE);
vol_center_text.setVisibility(View.GONE);
brightnessBarContainer.setVisibility(View.GONE);
volumeBarContainer.setVisibility(View.GONE);
onlySeekbar.setVisibility(View.VISIBLE);
root.setVisibility(View.VISIBLE);
calculatedTime = (int) (exoPlayer.getCurrentPosition() + (calculatedTime));
exoPlayer.seekTo(calculatedTime);
break;
}
return super.onTouchEvent(event);
}
}
to control brightness and volume by touch i managed to find https://github.com/moneytoo/Player which let me to do the function with more feature and uses the exoplayer library
and easy to build as same as exoplayer.
Related
I'm making an app where I'm using RecyclerView with SnapHelper class to make a scrollable horizontal card stack. I have implemented that successfully however my problem is that the cards swipe to right direction and I want to swipe the cards on left direction.
I did some research on Stack Overflow and found 1 and 2
But I still couldn't figure out how to set the scrolling position to left instead of right.
I also found that FindSnapView method passes the scroll position and direction maybe I'm wrong but below code might be the solution
#Override
public View findSnapView(RecyclerView.LayoutManager layoutManager) {
if (layoutManager instanceof LadderLayoutManager) {
int pos = ((LadderLayoutManager) layoutManager)
.getFixedScrollPosition(mDirection, mDirection != 0 ? 0.8f : 0.5f);
mDirection = 0;
if (pos != RecyclerView.NO_POSITION) {
return layoutManager.findViewByPosition(pos);
}
}
return null;
}
My main class
public class MainActivity extends AppCompatActivity {
LadderLayoutManager llm;
RecyclerView rcv;
HSAdapter adapter;
int scrollToPosition;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
llm = new LadderLayoutManager(1.5f, 0.85f, LadderLayoutManager.HORIZONTAL).
setChildDecorateHelper(new LadderLayoutManager
.DefaultChildDecorateHelper(getResources().getDimension(R.dimen.item_max_elevation)));
llm.setChildPeekSize((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
30, getResources().getDisplayMetrics()));
llm.setMaxItemLayoutCount(5);
rcv = (RecyclerView) findViewById(R.id.rcv);
rcv.setLayoutManager(llm);
new LadderSimpleSnapHelper().attachToRecyclerView(rcv);
adapter = new HSAdapter();
rcv.setAdapter(adapter);
final SeekBar sb = (SeekBar) findViewById(R.id.sb);
}
My code for custom LayoutManager
public class LadderLayoutManager extends RecyclerView.LayoutManager implements RecyclerView.SmoothScroller.ScrollVectorProvider {
private static final int INVALIDATE_SCROLL_OFFSET = Integer.MAX_VALUE;
private static final float DEFAULT_CHILD_LAYOUT_OFFSET = 0.2f;
public static final int UNLIMITED = 0;
public static final int VERTICAL = 1;
public static final int HORIZONTAL = 0;
private boolean mCheckedChildSize;
private int[] mChildSize;
private int mChildPeekSize;
private int mChildPeekSizeInput;
private boolean mReverse;
private int mScrollOffset = INVALIDATE_SCROLL_OFFSET;
private float mItemHeightWidthRatio;
private float mScale;
private int mChildCount;
private float mVanishOffset = 0;
private Interpolator mInterpolator;
private int mOrientation;
private ChildDecorateHelper mDecorateHelper;
private int mMaxItemLayoutCount;
public LadderLayoutManager(float itemHeightWidthRatio) {
this(itemHeightWidthRatio, 0.9f, VERTICAL);
}
public LadderLayoutManager(float itemHeightWidthRatio, float scale, int orientation) {
this.mItemHeightWidthRatio = itemHeightWidthRatio;
this.mOrientation = orientation;
this.mScale = scale;
this.mChildSize = new int[2];
this.mInterpolator = new DecelerateInterpolator();
}
#Override
public RecyclerView.LayoutParams generateDefaultLayoutParams() {
return new RecyclerView.LayoutParams(mChildSize[0], mChildSize[1]);
}
public LadderLayoutManager setChildDecorateHelper(ChildDecorateHelper layoutHelper) {
mDecorateHelper = layoutHelper;
return this;
}
public void setMaxItemLayoutCount(int count) {
mMaxItemLayoutCount = Math.max(2, count);
if (getChildCount() > 0) {
requestLayout();
}
}
public void setVanishOffset(float offset) {
mVanishOffset = offset;
if (getChildCount() > 0) {
requestLayout();
}
}
public void setChildPeekSize(int childPeekSize) {
mChildPeekSizeInput = childPeekSize;
mCheckedChildSize = false;
if (getChildCount() > 0) {
requestLayout();
}
}
public void setItemHeightWidthRatio(float itemHeightWidthRatio) {
mItemHeightWidthRatio = itemHeightWidthRatio;
mCheckedChildSize = false;
if (getChildCount() > 0) {
requestLayout();
}
}
public void setReverse(boolean reverse) {
if (mReverse != reverse) {
mReverse = reverse;
if (getChildCount() > 0) {
requestLayout();
}
}
}
public boolean isReverse() {
return mReverse;
}
public int getFixedScrollPosition(int direction, float fixValue) {
if (mCheckedChildSize) {
if (mScrollOffset % mChildSize[mOrientation] == 0) {
return RecyclerView.NO_POSITION;
}
float position = mScrollOffset * 1.0f / mChildSize[mOrientation];
return convert2AdapterPosition((int) (direction > 0 ? position + fixValue : position + (1 - fixValue)) - 1);
}
return RecyclerView.NO_POSITION;
}
#Override
public void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, int widthSpec, int heightSpec) {
super.onMeasure(recycler, state, widthSpec, heightSpec);
mCheckedChildSize = false;
}
#Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
if (state.getItemCount() == 0) {
removeAndRecycleAllViews(recycler);
return;
}
if (!mCheckedChildSize) {
if (mOrientation == VERTICAL) {
mChildSize[0] = getHorizontalSpace();
mChildSize[1] = (int) (mItemHeightWidthRatio * mChildSize[0]);
} else {
mChildSize[1] = getVerticalSpace();
mChildSize[0] = (int) (mChildSize[1] / mItemHeightWidthRatio);
}
mChildPeekSize = mChildPeekSizeInput == 0 ?
(int) (mChildSize[mOrientation] * DEFAULT_CHILD_LAYOUT_OFFSET) : mChildPeekSizeInput;
mCheckedChildSize = true;
}
int itemCount = getItemCount();
if (mReverse) {
mScrollOffset += (itemCount - mChildCount) * mChildSize[mOrientation];
}
mChildCount = itemCount;
mScrollOffset = makeScrollOffsetWithinRange(mScrollOffset);
fill(recycler);
}
public void fill(RecyclerView.Recycler recycler) {
int bottomItemPosition = (int) Math.floor(mScrollOffset / mChildSize[mOrientation]);//>=1
int bottomItemVisibleSize = mScrollOffset % mChildSize[mOrientation];
final float offsetPercent = mInterpolator.getInterpolation(
bottomItemVisibleSize * 1.0f / mChildSize[mOrientation]);//[0,1)
final int space = mOrientation == VERTICAL ? getVerticalSpace() : getHorizontalSpace();
ArrayList<ItemLayoutInfo> layoutInfos = new ArrayList<>();
for (int i = bottomItemPosition - 1, j = 1, remainSpace = space - mChildSize[mOrientation];
i >= 0; i--, j++) {
double maxOffset = mChildPeekSize * Math.pow(mScale, j);
int start = (int) (remainSpace - offsetPercent * maxOffset);
ItemLayoutInfo info = new ItemLayoutInfo(start,
(float) (Math.pow(mScale, j - 1) * (1 - offsetPercent * (1 - mScale))),
offsetPercent,
start * 1.0f / space
);
layoutInfos.add(0, info);
if (mMaxItemLayoutCount != UNLIMITED && j == mMaxItemLayoutCount - 1) {
if (offsetPercent != 0) {
info.start = remainSpace;
info.positionOffsetPercent = 0;
info.layoutPercent = remainSpace / space;
info.scaleXY = (float) Math.pow(mScale, j - 1);
}
break;
}
remainSpace -= maxOffset;
if (remainSpace <= 0) {
info.start = (int) (remainSpace + maxOffset);
info.positionOffsetPercent = 0;
info.layoutPercent = info.start / space;
info.scaleXY = (float) Math.pow(mScale, j - 1);
break;
}
}
if (bottomItemPosition < mChildCount) {
final int start = space - bottomItemVisibleSize;
layoutInfos.add(new ItemLayoutInfo(start, 1.0f,
bottomItemVisibleSize * 1.0f / mChildSize[mOrientation], start * 1.0f / space).
setIsBottom());
} else {
bottomItemPosition -= 1;
}
int layoutCount = layoutInfos.size();
final int startPos = bottomItemPosition - (layoutCount - 1);
final int endPos = bottomItemPosition;
final int childCount = getChildCount();
for (int i = childCount - 1; i >= 0; i--) {
View childView = getChildAt(i);
int pos = convert2LayoutPosition(getPosition(childView));
if (pos > endPos || pos < startPos) {
removeAndRecycleView(childView, recycler);
}
}
detachAndScrapAttachedViews(recycler);
for (int i = 0; i < layoutCount; i++) {
fillChild(recycler.getViewForPosition(convert2AdapterPosition(startPos + i)), layoutInfos.get(i));
}
}
private void fillChild(View view, ItemLayoutInfo layoutInfo) {
addView(view);
measureChildWithExactlySize(view);
final int scaleFix = (int) (mChildSize[mOrientation] * (1 - layoutInfo.scaleXY) / 2);
final float gap = (mOrientation == VERTICAL ? getHorizontalSpace() : getVerticalSpace())
- mChildSize[(mOrientation + 1) % 2] * layoutInfo.scaleXY;
if (mOrientation == VERTICAL) {
int left = (int) (getPaddingLeft() + (gap * 0.5 * mVanishOffset));
layoutDecoratedWithMargins(view, left, layoutInfo.start - scaleFix
, left + mChildSize[0], layoutInfo.start + mChildSize[1] - scaleFix);
} else {
int top = (int) (getPaddingTop() + (gap * 0.5 * mVanishOffset));
layoutDecoratedWithMargins(view, layoutInfo.start - scaleFix, top
, layoutInfo.start + mChildSize[0] - scaleFix, top + mChildSize[1]);
}
ViewCompat.setScaleX(view, layoutInfo.scaleXY);
ViewCompat.setScaleY(view, layoutInfo.scaleXY);
if (mDecorateHelper != null) {
mDecorateHelper.decorateChild(view, layoutInfo.positionOffsetPercent, layoutInfo.layoutPercent, layoutInfo.isBottom);
}
}
private void measureChildWithExactlySize(View child) {
RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams) child.getLayoutParams();
final int widthSpec = View.MeasureSpec.makeMeasureSpec(
mChildSize[0] - lp.leftMargin - lp.rightMargin, View.MeasureSpec.EXACTLY);
final int heightSpec = View.MeasureSpec.makeMeasureSpec(
mChildSize[1] - lp.topMargin - lp.bottomMargin, View.MeasureSpec.EXACTLY);
child.measure(widthSpec, heightSpec);
}
private int makeScrollOffsetWithinRange(int scrollOffset) {
return Math.min(Math.max(mChildSize[mOrientation], scrollOffset), mChildCount * mChildSize[mOrientation]);
}
#Override
public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) {
int pendingScrollOffset = mScrollOffset + dy;
mScrollOffset = makeScrollOffsetWithinRange(pendingScrollOffset);
fill(recycler);
return mScrollOffset - pendingScrollOffset + dy;
}
#Override
public int scrollHorizontallyBy(int dx, RecyclerView.Recycler recycler, RecyclerView.State state) {
int pendingScrollOffset = mScrollOffset + dx;
mScrollOffset = makeScrollOffsetWithinRange(pendingScrollOffset);
fill(recycler);
return mScrollOffset - pendingScrollOffset + dx;
}
#Override
public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
final LinearSmoothScroller linearSmoothScroller = new LinearSmoothScroller(recyclerView.getContext()) {
#Override
public int calculateDyToMakeVisible(final View view, final int snapPreference) {
if (mOrientation == VERTICAL) {
return -calculateDistanceToPosition(getPosition(view));
}
return 0;
}
#Override
public int calculateDxToMakeVisible(final View view, final int snapPreference) {
if (mOrientation == HORIZONTAL) {
return -calculateDistanceToPosition(getPosition(view));
}
return 0;
}
};
linearSmoothScroller.setTargetPosition(position);
startSmoothScroll(linearSmoothScroller);
}
public int calculateDistanceToPosition(int targetPos) {
int pendingScrollOffset = mChildSize[mOrientation] * (convert2LayoutPosition(targetPos) + 1);
return pendingScrollOffset - mScrollOffset;
}
#Override
public void scrollToPosition(int position) {
if (position > 0 && position < mChildCount) {
mScrollOffset = mChildSize[mOrientation] * (convert2LayoutPosition(position) + 1);
requestLayout();
}
}
#Override
public boolean canScrollVertically() {
return mOrientation == VERTICAL;
}
#Override
public boolean canScrollHorizontally() {
return mOrientation == HORIZONTAL;
}
public int convert2AdapterPosition(int layoutPosition) {
return mReverse ? mChildCount - 1 - layoutPosition : layoutPosition;
}
public int convert2LayoutPosition(int adapterPostion) {
return mReverse ? mChildCount - 1 - adapterPostion : adapterPostion;
}
public int getVerticalSpace() {
return getHeight() - getPaddingTop() - getPaddingBottom();
}
public int getHorizontalSpace() {
return getWidth() - getPaddingLeft() - getPaddingRight();
}
#Override
public PointF computeScrollVectorForPosition(int targetPosition) {
int pos = convert2LayoutPosition(targetPosition);
int scrollOffset = (pos + 1) * mChildSize[mOrientation];
return mOrientation == VERTICAL ? new PointF(0, Math.signum(scrollOffset - mScrollOffset))
: new PointF(Math.signum(scrollOffset - mScrollOffset), 0);
}
private static class ItemLayoutInfo {
float scaleXY;
float layoutPercent;
float positionOffsetPercent;
int start;
boolean isBottom;
ItemLayoutInfo(int top, float scale, float positonOffset, float percent) {
this.start = top;
this.scaleXY = scale;
this.positionOffsetPercent = positonOffset;
this.layoutPercent = percent;
}
ItemLayoutInfo setIsBottom() {
isBottom = true;
return this;
}
}
#Override
public Parcelable onSaveInstanceState() {
SavedState savedState = new SavedState();
savedState.scrollOffset = mScrollOffset;
savedState.reverse = mReverse;
savedState.vanishOffset = mVanishOffset;
savedState.scale = mScale;
savedState.childLayoutOffsetInput = mChildPeekSizeInput;
savedState.itemHeightWidthRatio = mItemHeightWidthRatio;
savedState.orientation = mOrientation;
return savedState;
}
#Override
public void onRestoreInstanceState(Parcelable state) {
if (state instanceof SavedState) {
SavedState s = (SavedState) state;
mScrollOffset = s.scrollOffset;
mReverse = s.reverse;
mVanishOffset = s.vanishOffset;
mScale = s.scale;
mChildPeekSizeInput = s.childLayoutOffsetInput;
mItemHeightWidthRatio = s.itemHeightWidthRatio;
mOrientation = s.orientation;
requestLayout();
}
}
public static class SavedState implements Parcelable {
int scrollOffset, childLayoutOffsetInput, orientation;
float itemHeightWidthRatio, scale, elevation, vanishOffset;
boolean reverse;
public SavedState() {
}
SavedState(LadderLayoutManager.SavedState other) {
scrollOffset = other.scrollOffset;
childLayoutOffsetInput = other.childLayoutOffsetInput;
orientation = other.orientation;
itemHeightWidthRatio = other.itemHeightWidthRatio;
scale = other.scale;
elevation = other.elevation;
vanishOffset = other.vanishOffset;
reverse = other.reverse;
}
SavedState(Parcel in) {
scrollOffset = in.readInt();
childLayoutOffsetInput = in.readInt();
orientation = in.readInt();
itemHeightWidthRatio = in.readFloat();
scale = in.readFloat();
elevation = in.readFloat();
vanishOffset = in.readFloat();
reverse = in.readInt() == 1;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(scrollOffset);
dest.writeInt(childLayoutOffsetInput);
dest.writeInt(orientation);
dest.writeFloat(itemHeightWidthRatio);
dest.writeFloat(scale);
dest.writeFloat(elevation);
dest.writeFloat(vanishOffset);
dest.writeInt(reverse ? 1 : 0);
}
#Override
public int describeContents() {
return 0;
}
public static final Creator<SavedState> CREATOR
= new Creator<SavedState>() {
#Override
public LadderLayoutManager.SavedState createFromParcel(Parcel in) {
return new LadderLayoutManager.SavedState(in);
}
#Override
public LadderLayoutManager.SavedState[] newArray(int size) {
return new LadderLayoutManager.SavedState[size];
}
};
}
public interface ChildDecorateHelper {
void decorateChild(View child, float posOffsetPercent, float layoutPercent, boolean isBottom);
}
public static class DefaultChildDecorateHelper implements ChildDecorateHelper {
private float mElevation;
public DefaultChildDecorateHelper(float maxElevation) {
mElevation = maxElevation;
}
#Override
public void decorateChild(View child, float posOffsetPercent, float layoutPercent, boolean isBottom) {
ViewCompat.setElevation(child, (float) (layoutPercent * mElevation * 0.7 + mElevation * 0.3));
}
}
}
Here is my code for snaphelper
public class LadderSimpleSnapHelper extends SnapHelper {
private int mDirection;
//int position = layoutManager.getPosition(centerView);
#Override
public int[] calculateDistanceToFinalSnap(
#NonNull RecyclerView.LayoutManager layoutManager, #NonNull View targetView) {
if (layoutManager instanceof LadderLayoutManager) {
int[] out = new int[2];
if (layoutManager.canScrollHorizontally()) {
out[0] = ((LadderLayoutManager) layoutManager).calculateDistanceToPosition(
layoutManager.getPosition(targetView));
out[1] = 0;
} else {
out[0] = 0;
out[1] = ((LadderLayoutManager) layoutManager).calculateDistanceToPosition(
layoutManager.getPosition(targetView));
}
return out;
}
return null;
}
#Override
public int findTargetSnapPosition(RecyclerView.LayoutManager layoutManager, int velocityX,
int velocityY) {
if (layoutManager.canScrollHorizontally()) {
mDirection = velocityX;
} else {
mDirection = velocityY;
}
return RecyclerView.NO_POSITION;
}
#Override
public View findSnapView(RecyclerView.LayoutManager layoutManager) {
if (layoutManager instanceof LadderLayoutManager) {
int pos = ((LadderLayoutManager) layoutManager).getFixedScrollPosition(
mDirection, mDirection != 0 ? 0.8f : 0.5f);
mDirection = 0;
if (pos != RecyclerView.NO_POSITION) {
return layoutManager.findViewByPosition(pos);
}
}
return null;
}
}
try below code :
mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, true));
mRecyclerView.setReverseLayout(true);
Use the code below:
mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, true));
in xml:
right to left
android:layoutDirection="rtl"
left to right
android:layoutDirection="ltr"
I have a piece of code that compares to images and places a marker on the difference. So far it works well, except the latest marker layer that is added always shows underneath all the older markers. I have the latest one as a yellow color and the older ones as red. When the difference is close to one of the red markers, the yellow marker shows behind those ones.
Is there anyone that can help me get the yellow (Latest marker) to appear on top?
This is my code so far:
public class CheckmarkActivity extends AppCompatActivity implements ZoomLayout.OnZoomableLayoutClickEventListener {
TextView tv;
RelativeLayout relativeLayout_work;
ImageView imageViewtest;
Bitmap prevBmp = null;
Timer t;
TimerTask task;
int time = 100;
float image_Width;
float image_Height;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checkmark);
if (getResources().getBoolean(R.bool.is_tablet)) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
tv = findViewById(R.id.tv);
relativeLayout_work = findViewById(R.id.relativeLayout_work);
imageViewtest = findViewById(R.id.imageViewtest);
prevBmp = ViewcontrollerActivity.workSession.getLastScreenShot();
if (prevBmp == null || ViewcontrollerActivity.workSession.workScreenShot == null) {
setResult(Activity.RESULT_CANCELED);
finish();
}
startTimer();
}
// image compare
class TestAsync extends AsyncTask<Object, Integer, String>
{
String TAG = getClass().getSimpleName();
PointF p;
Bitmap test_3;
protected void onPreExecute (){
super.onPreExecute();
Log.d(TAG + " PreExceute","On pre Exceute......");
}
protected String doInBackground(Object...arg0) {
test_3 = ImageHelper.findDifference(CheckmarkActivity.this, prevBmp, ViewcontrollerActivity.workSession.workScreenShot);
p = ImageHelper.findShot(test_3);
time = 1;
return "You are at PostExecute";
}
protected void onProgressUpdate(Integer...a){
super.onProgressUpdate(a);
}
protected void onPostExecute(String result) {
super.onPostExecute(result);
addImageToImageview();
PointF np = Session.convertPointBitmap2View(p, relativeLayout_work, ViewcontrollerActivity.workSession.workScreenShot);
tv.setX(np.x - tv.getWidth() / 2);
tv.setY(np.y - tv.getHeight() / 2);
tv.setVisibility(View.VISIBLE);
// imageViewtest.setImageBitmap(test_3);
}
}
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
#Override
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS:
{
Log.i("OpenCV", "OpenCV loaded successfully");
new TestAsync().execute();
} break;
default:
{
super.onManagerConnected(status);
} break;
}
}
};
#Override
protected void onResume() {
super.onResume();
if (!OpenCVLoader.initDebug()) {
Log.d("OpenCV", "Internal OpenCV library not found. Using OpenCV Manager for initialization");
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_0_0, this, mLoaderCallback);
} else {
Log.d("OpenCV", "OpenCV library found inside package. Using it!");
mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
}
}
public static int[] getBitmapOffset(ImageView img, Boolean includeLayout) {
int[] offset = new int[2];
float[] values = new float[9];
Matrix m = img.getImageMatrix();
m.getValues(values);
offset[0] = (int) values[5];
offset[1] = (int) values[2];
if (includeLayout) {
ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) img.getLayoutParams();
int paddingTop = (int) (img.getPaddingTop() );
int paddingLeft = (int) (img.getPaddingLeft() );
offset[0] += paddingTop + lp.topMargin;
offset[1] += paddingLeft + lp.leftMargin;
}
return offset;
}
public static int[] getBitmapPositionInsideImageView(ImageView imageView) {
int[] ret = new int[4];
if (imageView == null || imageView.getDrawable() == null)
return ret;
// Get image dimensions
// Get image matrix values and place them in an array
float[] f = new float[9];
imageView.getImageMatrix().getValues(f);
// Extract the scale values using the constants (if aspect ratio maintained, scaleX == scaleY)
final float scaleX = f[Matrix.MSCALE_X];
final float scaleY = f[Matrix.MSCALE_Y];
// Get the drawable (could also get the bitmap behind the drawable and getWidth/getHeight)
final Drawable d = imageView.getDrawable();
final int origW = d.getIntrinsicWidth();
final int origH = d.getIntrinsicHeight();
// Calculate the actual dimensions
final int actW = Math.round(origW * scaleX);
final int actH = Math.round(origH * scaleY);
ret[2] = actW;
ret[3] = actH;
// Get image position
// We assume that the image is centered into ImageView
int imgViewW = imageView.getWidth();
int imgViewH = imageView.getHeight();
int top = (int) (imgViewH - actH)/2;
int left = (int) (imgViewW - actW)/2;
ret[0] = left;
ret[1] = top;
return ret;
}
private void addImageToImageview(){
if (ViewcontrollerActivity.workSession.workScreenShot != null) {
imageViewtest.setImageBitmap(ViewcontrollerActivity.workSession.workScreenShot);
Log.d("width", String.valueOf(imageViewtest.getWidth()));
}
Resources r = getResources();
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, r.getDisplayMetrics());
for (int i = 0; i < ViewcontrollerActivity.workSession.getShotCount(); i++) {
PointF p = ViewcontrollerActivity.workSession.getPoint(i);
TextView t = new TextView(this);
t.setText("" + (i + 1));
RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams((int)px, (int)px);
relativeLayout_work.addView(t);
t.setLayoutParams(param);
t.setGravity(Gravity.CENTER);
t.setBackgroundResource(R.drawable.circle);
p = Session.convertPointBitmap2View(p, relativeLayout_work, ViewcontrollerActivity.workSession.workScreenShot);
t.setX(p.x);
t.setY(p.y);
t.setTag(10000 + i);
}
}
public void onConfirm(View v){
View vv = findViewById(R.id.relativeLayout_work);
PointF bp = Session.convertPointView2Bitmap(new PointF(tv.getX(), tv.getY()), relativeLayout_work, ViewcontrollerActivity.workSession.workScreenShot);
ViewcontrollerActivity.workSession.addNewShot(ViewcontrollerActivity.workSession.workScreenShot, bp);
setResult(Activity.RESULT_OK);
finish();
}
public void onCancel(View v){
setResult(Activity.RESULT_CANCELED);
finish();
}
#Override
public void onBackPressed() {
setResult(Activity.RESULT_CANCELED);
finish();
}
#Override
public void OnContentClickEvent(int action, float xR, float yR) {
int[] offset = new int[2];
int[] rect = new int[4];
offset = this.getBitmapOffset(imageViewtest, false);
int original_width = imageViewtest.getDrawable().getIntrinsicWidth();
int original_height = imageViewtest.getDrawable().getIntrinsicHeight();
rect = getBitmapPositionInsideImageView(imageViewtest);
Log.i("OffsetY", String.valueOf(offset[0]));
Log.i("OffsetX", String.valueOf(offset[1]));
Log.i( "0", String.valueOf(rect[0]));
Log.i( "1", String.valueOf(rect[1]));
Log.i( "2", String.valueOf(rect[2]));
Log.i( "3", String.valueOf(rect[3]));
if (xR > rect[0] && xR < rect[0] + rect[2] && yR > rect[1] && yR < rect[1] + rect[3]) {
tv.setX(xR - tv.getWidth() / 2);
tv.setY(yR - tv.getHeight() / 2);
}
// tv.setX(xR - tv.getWidth() / 2);
// tv.setY(yR - tv.getHeight() / 2);
}
public void onMoveButtonPressed(View v) {
ImageButton b = (ImageButton)v;
int mId = b.getId();
switch (mId) {
case R.id.imageButtonL:
tv.setX(tv.getX() - 1);
break;
case R.id.imageButtonR:
tv.setX(tv.getX() + 1);
break;
case R.id.imageButtonD:
tv.setY(tv.getY() + 1);
break;
case R.id.imageButtonU:
tv.setY(tv.getY() - 1);
break;
}
}
//timer change image
public void startTimer(){
t = new Timer();
task = new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
if (time == 1){
imageViewtest.setImageBitmap(ViewcontrollerActivity.workSession.workScreenShot);
// tv.setVisibility(View.VISIBLE);
tv.setText("" + (ViewcontrollerActivity.workSession.getShotCount() + 1));
t.cancel();
return;
}
if (time % 2 == 0) {
imageViewtest.setImageBitmap(prevBmp);
}
else if(time % 2 == 1){
imageViewtest.setImageBitmap(ViewcontrollerActivity.workSession.workScreenShot);
}
time --;
}
});
}
};
t.scheduleAtFixedRate(task, 0, 500);
}
}
You can give the z-order of the child view with addView() function.
void addView (View child, int index)
ex)
private void addImageToImageview(){
if (ViewcontrollerActivity.workSession.workScreenShot != null) {
imageViewtest.setImageBitmap(ViewcontrollerActivity.workSession.workScreenShot);
Log.d("width", String.valueOf(imageViewtest.getWidth()));
}
Resources r = getResources();
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, r.getDisplayMetrics());
int currChildrenCount = relativeLayout_work.getChildCount();
for (int i = 0; i < ViewcontrollerActivity.workSession.getShotCount(); i++) {
PointF p = ViewcontrollerActivity.workSession.getPoint(i);
TextView t = new TextView(this);
t.setText("" + (i + 1));
RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams((int)px, (int)px);
relativeLayout_work.addView(t, currChildrenCount+i); // You can control the order like this
t.setLayoutParams(param);
t.setGravity(Gravity.CENTER);
t.setBackgroundResource(R.drawable.circle);
p = Session.convertPointBitmap2View(p, relativeLayout_work, ViewcontrollerActivity.workSession.workScreenShot);
t.setX(p.x);
t.setY(p.y);
t.setTag(10000 + i);
}
}
I used this to perform pinch zoom and drag.
public class ZoomLayout extends FrameLayout implements ScaleGestureDetector.OnScaleGestureListener {
private enum Mode {
NONE,
DRAG,
ZOOM
}
private static final String TAG = "ZoomLayout";
private static final float MIN_ZOOM = 1.0f;
private static final float MAX_ZOOM = 4.0f;
private Mode mode = Mode.NONE;
private float scale = 1.0f;
private float lastScaleFactor = 0f;
// Where the finger first touches the screen
private float startX = 0f;
private float startY = 0f;
// How much to translate the canvas
private float dx = 0f;
private float dy = 0f;
private float prevDx = 0f;
private float prevDy = 0f;
public ZoomLayout(Context context) {
super(context);
init(context);
}
public ZoomLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public ZoomLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
private void init(Context context) {
final ScaleGestureDetector scaleDetector = new ScaleGestureDetector(context, this);
this.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
Log.i(TAG, "DOWN");
if (scale > MIN_ZOOM) {
mode = Mode.DRAG;
startX = motionEvent.getX() - prevDx;
startY = motionEvent.getY() - prevDy;
}
break;
case MotionEvent.ACTION_MOVE:
if (mode == Mode.DRAG) {
dx = motionEvent.getX() - startX;
dy = motionEvent.getY() - startY;
}
break;
case MotionEvent.ACTION_POINTER_DOWN:
mode = Mode.ZOOM;
break;
case MotionEvent.ACTION_POINTER_UP:
mode = Mode.DRAG;
break;
case MotionEvent.ACTION_UP:
Log.i(TAG, "UP");
mode = Mode.NONE;
prevDx = dx;
prevDy = dy;
break;
}
scaleDetector.onTouchEvent(motionEvent);
if ((mode == Mode.DRAG && scale >= MIN_ZOOM) || mode == Mode.ZOOM) {
getParent().requestDisallowInterceptTouchEvent(true);
float maxDx = (child().getWidth() - (child().getWidth() / scale)) / 2 * scale;
float maxDy = (child().getHeight() - (child().getHeight() / scale))/ 2 * scale;
dx = Math.min(Math.max(dx, -maxDx), maxDx);
dy = Math.min(Math.max(dy, -maxDy), maxDy);
Log.i(TAG, "Width: " + child().getWidth() + ", scale " + scale + ", dx " + dx
+ ", max " + maxDx);
applyScaleAndTranslation();
}
return true;
}
});
}
// ScaleGestureDetector
#Override
public boolean onScaleBegin(ScaleGestureDetector scaleDetector) {
Log.i(TAG, "onScaleBegin");
return true;
}
#Override
public boolean onScale(ScaleGestureDetector scaleDetector) {
float scaleFactor = scaleDetector.getScaleFactor();
Log.i(TAG, "onScale" + scaleFactor);
if (lastScaleFactor == 0 || (Math.signum(scaleFactor) == Math.signum(lastScaleFactor))) {
scale *= scaleFactor;
scale = Math.max(MIN_ZOOM, Math.min(scale, MAX_ZOOM));
lastScaleFactor = scaleFactor;
} else {
lastScaleFactor = 0;
}
return true;
}
#Override
public void onScaleEnd(ScaleGestureDetector scaleDetector) {
Log.i(TAG, "onScaleEnd");
}
private void applyScaleAndTranslation() {
child().setScaleX(scale);
child().setScaleY(scale);
child().setTranslationX(dx);
child().setTranslationY(dy);
}
private View child() {
return getChildAt(0);
}
}
The problem that I am having with this is that when I zoom in, then drag, it snaps to the middle of the screen. I think the problem lies at:
case MotionEvent.ACTION_DOWN:
Log.i(TAG, "DOWN");
if (scale > MIN_ZOOM) {
mode = Mode.DRAG;
startX = motionEvent.getX() - prevDx;
startY = motionEvent.getY() - prevDy;
}
break;
I am not sure how to fix this, can anyone please help me?
I fixed it by changing onScale to:
public boolean onScale(ScaleGestureDetector scaleDetector) {
float scaleFactor = scaleDetector.getScaleFactor();
Log.i(TAG, "mode:" + this.mode + ", onScale:" + scaleFactor);
if (this.lastScaleFactor == 0.0f || Math.signum(scaleFactor) == Math.signum(this.lastScaleFactor)) {
this.scale *= scaleFactor;
this.scale = Math.max(MIN_ZOOM, Math.min(this.scale, MAX_ZOOM));
this.lastScaleFactor = scaleFactor;
} else {
this.lastScaleFactor = 0.0f;
}
if (this.mSurfaceView != null) {
int orgWidth = getWidth();
int _width = (int) (((float) orgWidth) * this.scale);
int _height = (int) (((float) getHeight()) * this.scale);
LogUtil.b(new StringBuilder(String.valueOf(_width)).append(", ").append(_height).toString());
LayoutParams params = (LayoutParams) this.mSurfaceView.getLayoutParams();
params.height = _height;
params.width = _width;
this.mSurfaceView.setLayoutParams(params);
child().setScaleX(this.scale);
child().setScaleY(this.scale);
}
return true;
}
I recently started using a canvas over a layout because I could have animations in the background. However, I want to start a new activity after the user clicks a buttons. Here is the code so far...
public class GFX extends Activity implements View.OnTouchListener {
MyBringBack ourSurfaceView;
Paint title, play, options;
float x, y, sX, sY, fX, fY, dX, dY, aniX, aniY, scaledX, scaledY, changingRedX, changingYellowX, changingPurpleX, changingGreenX, whereIsRedXY, whereIsYellowXY, whereIsGreenXY, whereIsPurpleXY;
int whereIsRedY, redXThing, whereIsYellowY, whereIsGreenY, whereIsPurpleY, yellowXThing, greenXThing, purpleXThing, firstRun;
Bitmap green, yellow, red, purple, plus, redFixed, yellowFixed, greenFixed, purpleFixed, titleTest, playTest, playFixed;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ourSurfaceView = new MyBringBack(this);
ourSurfaceView.setOnTouchListener(this);
x = 0;
y = 0;
sX = 0;
sY = 0;
fX = 0;
fY = 0;
Context ctx;
firstRun = 0;
dX = dY = aniX = aniY = scaledX = scaledY = redXThing = 0;
ctx = this;
green = BitmapFactory.decodeResource(getResources(), R.drawable.greenhairedpotatoblack);
yellow = BitmapFactory.decodeResource(getResources(), R.drawable.yellowhairedpotatoblack);
red = BitmapFactory.decodeResource(getResources(), R.drawable.redhairedpotatoblack);
purple = BitmapFactory.decodeResource(getResources(), R.drawable.purplehairedpotatoblack);
plus = BitmapFactory.decodeResource(getResources(), R.drawable.plus);
titleTest = BitmapFactory.decodeResource(getResources(), R.drawable.title);
playTest = BitmapFactory.decodeResource(getResources(), R.drawable.play);
setContentView(ourSurfaceView);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_gfx, menu);
return true;
}
#Override
protected void onPause() {
super.onPause();
ourSurfaceView.pause();
}
#Override
protected void onResume() {
super.onResume();
ourSurfaceView.resume();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public boolean onTouch(View v, MotionEvent event) {
try {
Thread.sleep(1000/60);
} catch (InterruptedException e) {
e.printStackTrace();
}
x = event.getX();
y = event.getY();
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
//if(y > play.getTextBounds("Play", );)
break;
}
return true;
}
public class MyBringBack extends SurfaceView implements Runnable {
SurfaceHolder ourHolder;
Thread ourThread = null;
boolean isRunning = false;
public MyBringBack(Context context) {
super(context);
ourHolder = getHolder();
}
public void pause(){
isRunning = false;
while(true){
try {
ourThread.join();
} catch (InterruptedException e){
e.printStackTrace();
}
break;
}
ourThread = null;
}
public void resume(){
isRunning = true;
ourThread = new Thread(this);
ourThread.start();
}
#Override
public void run() {
while(isRunning){
if(!ourHolder.getSurface().isValid())
continue;
Canvas canvas = ourHolder.lockCanvas();
canvas.drawColor(Color.BLACK);
if(firstRun == 0){
changingRedX = 0 - red.getWidth();
yellowXThing = 1;
changingYellowX = canvas.getWidth();
greenXThing = 1;
firstRun = 1;
title = new Paint();
title.setColor(Color.parseColor("#0889ec"));
title.setTextSize(180);
title.setTextAlign(Paint.Align.CENTER);
title.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));
play = new Paint();
play.setTextSize(140);
play.setColor(Color.WHITE);
play.setTextAlign(Paint.Align.CENTER);
options = new Paint();
options.setTextSize(140);
options.setColor(Color.WHITE);
options.setTextAlign(Paint.Align.CENTER);
playFixed = getResizedBitmap(playTest, canvas.getWidth()/5, canvas.getHeight()/3);
redFixed = getResizedBitmap(red, canvas.getWidth()/6, (int)canvas.getWidth()/6 * (red.getHeight()/red.getWidth()));
yellowFixed = getResizedBitmap(yellow, canvas.getWidth()/6, (int)canvas.getWidth()/6 * (yellow.getHeight()/yellow.getWidth()));
greenFixed = getResizedBitmap(green, canvas.getWidth()/6, (int)canvas.getWidth()/6 * (green.getHeight()/green.getWidth()));
purpleFixed = getResizedBitmap(purple, canvas.getWidth()/6, (int)canvas.getWidth()/6 * (purple.getHeight()/purple.getWidth()));
}
if((x>(canvas.getWidth()/2)-(playFixed.getWidth()/2)&&(x<(canvas.getWidth()/2)-(playFixed.getWidth()/2) + playFixed.getWidth()))
&& ((y > 4*(canvas.getHeight()/9))&& (y<4*(canvas.getHeight()/9)+playFixed.getHeight()))){
**** HERE IS WHERE I WANT TO START THE NEW INTENT ****
}
if(whereIsRedY == 0){
whereIsRedXY = (int) (Math.random() * canvas.getHeight()) - 20;
whereIsRedY = 1;
}
if(whereIsYellowY == 0){
whereIsYellowXY = (int) (Math.random() * canvas.getHeight()) - 20;
whereIsYellowY = 1;
}
if(whereIsGreenY == 0){
whereIsGreenXY = (int) (Math.random() * canvas.getWidth()) - 20;
whereIsGreenY = 1;
}
if(whereIsPurpleY == 0){
whereIsPurpleXY = (int) (Math.random() * canvas.getWidth()) - 20;
whereIsPurpleY = 1;
}
if((changingRedX > canvas.getWidth() + redFixed.getWidth()) && redXThing == 0){
changingRedX = canvas.getWidth() + 3*(redFixed.getWidth());
whereIsRedY = 0;
redXThing = 1;
}
if((changingRedX < 0 - redFixed.getWidth()) && redXThing == 1){
changingRedX = 0 - 4*(redFixed.getWidth());
whereIsRedY = 0;
redXThing = 0;
}
if(redXThing == 0) {
changingRedX += 10;
}
if(redXThing == 1) {
changingRedX -= 10;
}
if((changingYellowX > canvas.getWidth() + yellowFixed.getWidth()) && yellowXThing == 0){
changingYellowX = canvas.getWidth() + 3*(yellowFixed.getWidth());
whereIsYellowY = 0;
yellowXThing = 1;
}
if((changingYellowX < 0 - yellowFixed.getWidth()) && yellowXThing == 1){
changingYellowX = 0 - 4*(yellowFixed.getWidth());
whereIsYellowY = 0;
yellowXThing = 0;
}
if(yellowXThing == 0) {
changingYellowX += 13;
}
if(yellowXThing == 1) {
changingYellowX -= 13;
}
if((changingGreenX > canvas.getHeight() + greenFixed.getHeight()) && greenXThing == 0){
changingGreenX = canvas.getHeight() + 3*(greenFixed.getHeight());
whereIsGreenY = 0;
greenXThing = 1;
}
if((changingGreenX < 0 - greenFixed.getHeight()) && greenXThing == 1){
changingGreenX = 0 - 4*(greenFixed.getHeight());
whereIsGreenY = 0;
greenXThing = 0;
}
if(greenXThing == 0) {
changingGreenX += 8;
}
if(greenXThing == 1) {
changingGreenX -= 8;
}
if((changingPurpleX > canvas.getHeight() + purpleFixed.getHeight()) && purpleXThing == 0){
changingPurpleX = canvas.getHeight() + 3*(purpleFixed.getHeight());
whereIsPurpleY = 0;
purpleXThing = 1;
}
if((changingPurpleX < 0 - purpleFixed.getHeight()) && purpleXThing == 1){
changingPurpleX = 0 - 4*(purpleFixed.getHeight());
whereIsPurpleY = 0;
purpleXThing = 0;
}
if(purpleXThing == 0) {
changingPurpleX += 15;
}
if(purpleXThing == 1) {
changingPurpleX -= 15;
}
canvas.drawBitmap(redFixed, changingRedX, whereIsRedXY, null);
canvas.drawBitmap(yellowFixed, changingYellowX, whereIsYellowXY, null);
canvas.drawBitmap(purpleFixed, whereIsPurpleXY, changingPurpleX, null);
canvas.drawBitmap(greenFixed, whereIsGreenXY, changingGreenX, null);
canvas.drawText("Mustache Me", (canvas.getWidth() ) / 2, (canvas.getHeight()/3), title);
//canvas.drawText("Play", canvas.getWidth()/2, canvas.getHeight()/2 + 40, play);
canvas.drawBitmap(playFixed, (canvas.getWidth()/2)-(playFixed.getWidth()/2), 4*(canvas.getHeight()/9), null);
canvas.drawText("Options", canvas.getWidth()/2, 2*(canvas.getHeight()/3), options);
ourHolder.unlockCanvasAndPost(canvas);
}
}
}
public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth)
{
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// create a matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
return resizedBitmap;
}
}
I labeled in my code where I want to intent to start, yet I keep getting the error: "Cannot resolve constructor Intetn(.....)"
The code I tried was this:
Intent i = new Intent(this, WhichGameActivity.class);
Thanks for any help :)
Cheers.
Use this:
new Intent(GFX.this,WhichGameActivity.class);
You can't just use this because you're inside a inner class, thus this belongs to the inner class context.
Pay attention to what Docs say about Intent constructor:
public Intent (Context packageContext, Class cls)
Create an intent for a specific component. ...
Parameters
packageContext A Context of the application package implementing this
class. cls The component class that is to be used for the
intent.
Your class MyBringBack is not a Context,so you can not use this code:
Intent i = new Intent(this, WhichGameActivity.class);
You have to store context that is received in constructor of MyBringBack as a field for example "myContext" and then use it in new Intent() like this:
Intent i = new Intent(myContext, WhichGameActivity.class);
How to animate Cards in android Card Game Like "Spades Free". My game is quite the same as crazy 8.i used android game development for dummies as a reference.but, i want to animate cards how can i do that.
public class GameView extends View {
private int screenW;
private int screenH;
private Context myContext;
private List<Card> deck = new ArrayList<Card>();
private int scaledCardW;
private int scaledCardH;
private Paint whitePaint;
private List<Card> myHand = new ArrayList<Card>();
private List<Card> oppHand = new ArrayList<Card>();
private int myScore = 0;
private int oppScore = 0;
private float scale;
private Bitmap cardBack;
private List<Card> discardPile = new ArrayList<Card>();
private boolean myTurn;
private ComputerPlayer computerPlayer = new ComputerPlayer();
private int movingCardIdx = -1;
private int movingX;
private int movingY;
private int validRank = 8;
private int validSuit = 0;
private Bitmap nextCardButton;
private int scoreThisHand = 0;
public GameView(Context context) {
super(context);
myContext = context;
scale = myContext.getResources().getDisplayMetrics().density;
whitePaint = new Paint();
whitePaint.setAntiAlias(true);
whitePaint.setColor(Color.WHITE);
whitePaint.setStyle(Paint.Style.STROKE);
whitePaint.setTextAlign(Paint.Align.LEFT);
whitePaint.setTextSize(scale*15);
}
#Override
public void onSizeChanged (int w, int h, int oldw, int oldh){
super.onSizeChanged(w, h, oldw, oldh);
screenW = w;
screenH = h;
Bitmap tempBitmap = BitmapFactory.decodeResource(myContext.getResources(), R.drawable.card_back);
scaledCardW = (int) (screenW/8);
scaledCardH = (int) (scaledCardW*1.28);
cardBack = Bitmap.createScaledBitmap(tempBitmap, scaledCardW, scaledCardH, false);
nextCardButton = BitmapFactory.decodeResource(getResources(), R.drawable.arrow_next);
initCards();
dealCards();
drawCard(discardPile);
validSuit = discardPile.get(0).getSuit();
validRank = discardPile.get(0).getRank();
myTurn = new Random().nextBoolean();
if (!myTurn) {
makeComputerPlay();
}
}
#Override
protected void onDraw(Canvas canvas) {
canvas.drawText("Computer Score: " + Integer.toString(oppScore), 10, whitePaint.getTextSize()+10, whitePaint);
canvas.drawText("My Score: " + Integer.toString(myScore), 10, screenH-whitePaint.getTextSize()-10, whitePaint);
for (int i = 0; i < oppHand.size(); i++) {
canvas.drawBitmap(cardBack,
i*(scale*5),
whitePaint.getTextSize()+(50*scale),
null);
}
canvas.drawBitmap(cardBack, (screenW/2)-cardBack.getWidth()-10, (screenH/2)-(cardBack.getHeight()/2), null);
if (!discardPile.isEmpty()) {
canvas.drawBitmap(discardPile.get(0).getBitmap(),
(screenW/2)+10,
(screenH/2)-(cardBack.getHeight()/2),
null);
}
if (myHand.size() > 7) {
canvas.drawBitmap(nextCardButton,
screenW-nextCardButton.getWidth()-(30*scale),
screenH-nextCardButton.getHeight()-scaledCardH-(90*scale),
null);
}
for (int i = 0; i < myHand.size(); i++) {
if (i == movingCardIdx) {
canvas.drawBitmap(myHand.get(i).getBitmap(),
movingX,
movingY,
null);
} else {
if (i < 7) {
canvas.drawBitmap(myHand.get(i).getBitmap(),
i*(scaledCardW+5),
screenH-scaledCardH-whitePaint.getTextSize()-(50*scale),
null);
}
}
}
invalidate();
}
public boolean onTouchEvent(MotionEvent event) {
int eventaction = event.getAction();
int X = (int)event.getX();
int Y = (int)event.getY();
switch (eventaction ) {
case MotionEvent.ACTION_DOWN:
if (myTurn) {
for (int i = 0; i < 7; i++) {
if (X > i*(scaledCardW+5) && X < i*(scaledCardW+5) + scaledCardW &&
Y > screenH-scaledCardH-whitePaint.getTextSize()-(50*scale)) {
movingCardIdx = i;
movingX = X-(int)(30*scale);
movingY = Y-(int)(70*scale);
}
}
}
break;
case MotionEvent.ACTION_MOVE:
movingX = X-(int)(30*scale);
movingY = Y-(int)(70*scale);
break;
case MotionEvent.ACTION_UP:
if (movingCardIdx > -1 &&
X > (screenW/2)-(100*scale) &&
X < (screenW/2)+(100*scale) &&
Y > (screenH/2)-(100*scale) &&
Y < (screenH/2)+(100*scale) &&
(myHand.get(movingCardIdx).getRank() == 8 ||
myHand.get(movingCardIdx).getRank() == validRank ||
myHand.get(movingCardIdx).getSuit() == validSuit)) {
validRank = myHand.get(movingCardIdx).getRank();
validSuit = myHand.get(movingCardIdx).getSuit();
discardPile.add(0, myHand.get(movingCardIdx));
myHand.remove(movingCardIdx);
if (myHand.isEmpty()) {
endHand();
} else {
if (validRank == 8) {
showChooseSuitDialog();
} else {
myTurn = false;
makeComputerPlay();
}
}
}
if (movingCardIdx == -1 && myTurn &&
X > (screenW/2)-(100*scale) &&
X < (screenW/2)+(100*scale) &&
Y > (screenH/2)-(100*scale) &&
Y < (screenH/2)+(100*scale)) {
if (checkForValidDraw()) {
drawCard(myHand);
} else {
Toast.makeText(myContext, "You have a valid play.", Toast.LENGTH_SHORT).show();
}
}
if (myHand.size() > 7 &&
X > screenW-nextCardButton.getWidth()-(30*scale) &&
Y > screenH-nextCardButton.getHeight()-scaledCardH-(90*scale) &&
Y < screenH-nextCardButton.getHeight()-scaledCardH-(60*scale)) {
Collections.rotate(myHand, 1);
}
movingCardIdx = -1;
break;
}
invalidate();
return true;
}
private void showChooseSuitDialog() {
final Dialog chooseSuitDialog = new Dialog(myContext);
chooseSuitDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
chooseSuitDialog.setContentView(R.layout.choose_suit_dialog);
final Spinner suitSpinner = (Spinner) chooseSuitDialog.findViewById(R.id.suitSpinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
myContext, R.array.suits, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
suitSpinner.setAdapter(adapter);
Button okButton = (Button) chooseSuitDialog.findViewById(R.id.okButton);
okButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
validSuit = (suitSpinner.getSelectedItemPosition()+1)*100;
String suitText = "";
if (validSuit == 100) {
suitText = "Diamonds";
} else if (validSuit == 200) {
suitText = "Clubs";
} else if (validSuit == 300) {
suitText = "Hearts";
} else if (validSuit == 400) {
suitText = "Spades";
}
chooseSuitDialog.dismiss();
Toast.makeText(myContext, "You chose " + suitText, Toast.LENGTH_SHORT).show();
myTurn = false;
makeComputerPlay();
}
});
chooseSuitDialog.show();
}
private void initCards() {
for (int i = 0; i < 4; i++) {
for (int j = 102; j < 115; j++) {
int tempId = j + (i*100);
Card tempCard = new Card(tempId);
int resourceId = getResources().getIdentifier("card" + tempId, "drawable", myContext.getPackageName());
Bitmap tempBitmap = BitmapFactory.decodeResource(myContext.getResources(), resourceId);
scaledCardW = (int) (screenW/8);
scaledCardH = (int) (scaledCardW*1.28);
Bitmap scaledBitmap = Bitmap.createScaledBitmap(tempBitmap, scaledCardW, scaledCardH, false);
tempCard.setBitmap(scaledBitmap);
deck.add(tempCard);
}
}
}
private void drawCard(List<Card> handToDraw) {
handToDraw.add(0, deck.get(0));
deck.remove(0);
if (deck.isEmpty()) {
for (int i = discardPile.size()-1; i > 0 ; i--) {
deck.add(discardPile.get(i));
discardPile.remove(i);
Collections.shuffle(deck,new Random());
}
}
}
private void dealCards() {
Collections.shuffle(deck,new Random());
for (int i = 0; i < 7; i++) {
drawCard(myHand);
drawCard(oppHand);
}
}
private boolean checkForValidDraw() {
boolean canDraw = true;
for (int i = 0; i < myHand.size(); i++) {
int tempId = myHand.get(i).getId();
int tempRank = myHand.get(i).getRank();
int tempSuit = myHand.get(i).getSuit();
if (validSuit == tempSuit || validRank == tempRank ||
tempId == 108 || tempId == 208 || tempId == 308 || tempId == 408) {
canDraw = false;
}
}
return canDraw;
}
private void makeComputerPlay() {
int tempPlay = 0;
while (tempPlay == 0) {
tempPlay = computerPlayer.makePlay(oppHand, validSuit, validRank);
if (tempPlay == 0) {
drawCard(oppHand);
}
}
if (tempPlay == 108 || tempPlay == 208 || tempPlay == 308 || tempPlay == 408) {
validRank = 8;
validSuit = computerPlayer.chooseSuit(oppHand);
String suitText = "";
if (validSuit == 100) {
suitText = "Diamonds";
} else if (validSuit == 200) {
suitText = "Clubs";
} else if (validSuit == 300) {
suitText = "Hearts";
} else if (validSuit == 400) {
suitText = "Spades";
}
Toast.makeText(myContext, "Computer chose " + suitText, Toast.LENGTH_SHORT).show();
} else {
validSuit = Math.round((tempPlay/100) * 100);
validRank = tempPlay - validSuit;
}
for (int i = 0; i < oppHand.size(); i++) {
Card tempCard = oppHand.get(i);
if (tempPlay == tempCard.getId()) {
discardPile.add(0, oppHand.get(i));
oppHand.remove(i);
}
}
if (oppHand.isEmpty()) {
endHand();
}
myTurn = true;
}
}
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/pressed_center"
android:state_pressed="true" />
<item android:drawable="#drawable/unpressed_center" />
You should make draweble in same layout file for pres or not press version