-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed small bugs related to the old verison and added a first non per…
…formant rounded corner feature (it calls onDraw 4 times). Improvements needed before release.
- Loading branch information
Showing
33 changed files
with
2,008 additions
and
343 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
...geview-samples/src/main/java/com/cesards/samples/cropimageview/rounded_corners/Image.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.cesards.samples.cropimageview.rounded_corners; | ||
|
||
import com.cesards.cropimageview.crop.CropType; | ||
|
||
import androidx.annotation.DrawableRes; | ||
|
||
final class Image { | ||
|
||
@DrawableRes private final int drawableResource; | ||
private final boolean roundedCorners; | ||
@CropType private final int cropType; | ||
|
||
Image(@DrawableRes int drawableResource, boolean roundedCorners, int cropType) { | ||
this.drawableResource = drawableResource; | ||
this.roundedCorners = roundedCorners; | ||
this.cropType = cropType; | ||
} | ||
|
||
public int drawableResource() { | ||
return drawableResource; | ||
} | ||
|
||
public boolean roundedCorners() { | ||
return roundedCorners; | ||
} | ||
|
||
public int cropType() { | ||
return cropType; | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
...samples/src/main/java/com/cesards/samples/cropimageview/rounded_corners/ImageFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package com.cesards.samples.cropimageview.rounded_corners; | ||
|
||
import com.cesards.cropimageview.crop.CropType; | ||
import com.cesards.samples.cropimageview.R; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
final class ImageFactory { | ||
|
||
private ImageFactory() { | ||
throw new AssertionError("This shouldn't be initialized!"); | ||
} | ||
|
||
static List<Image> imagesWithoutRoundedCorners() { | ||
return Arrays.asList( | ||
// new Image(R.drawable.ball_vertical, false, CropType.NONE), | ||
// new Image(R.drawable.ball_horizontal, false, CropType.NONE), | ||
new Image(R.drawable.ball_vertical, false, CropType.CENTER_TOP), | ||
new Image(R.drawable.ball_vertical, false, CropType.CENTER_BOTTOM), | ||
new Image(R.drawable.ball_horizontal, false, CropType.LEFT_TOP), | ||
new Image(R.drawable.ball_horizontal, false, CropType.LEFT_CENTER), | ||
new Image(R.drawable.ball_horizontal, false, CropType.LEFT_BOTTOM), | ||
new Image(R.drawable.ball_horizontal, false, CropType.RIGHT_TOP), | ||
new Image(R.drawable.ball_horizontal, false, CropType.RIGHT_BOTTOM), | ||
new Image(R.drawable.ball_horizontal, false, CropType.RIGHT_CENTER), | ||
new Image(R.drawable.ball_vertical, false, CropType.CENTER_TOP), | ||
new Image(R.drawable.ball_vertical, false, CropType.CENTER_BOTTOM), | ||
new Image(R.drawable.ball_horizontal, false, CropType.LEFT_TOP), | ||
new Image(R.drawable.ball_horizontal, false, CropType.LEFT_CENTER), | ||
new Image(R.drawable.ball_horizontal, false, CropType.LEFT_BOTTOM), | ||
new Image(R.drawable.ball_horizontal, false, CropType.RIGHT_TOP), | ||
new Image(R.drawable.ball_horizontal, false, CropType.RIGHT_BOTTOM), | ||
new Image(R.drawable.ball_horizontal, false, CropType.RIGHT_CENTER), | ||
new Image(R.drawable.ball_vertical, false, CropType.CENTER_TOP), | ||
new Image(R.drawable.ball_vertical, false, CropType.CENTER_BOTTOM), | ||
new Image(R.drawable.ball_horizontal, false, CropType.LEFT_TOP), | ||
new Image(R.drawable.ball_horizontal, false, CropType.LEFT_CENTER), | ||
new Image(R.drawable.ball_horizontal, false, CropType.LEFT_BOTTOM), | ||
new Image(R.drawable.ball_horizontal, false, CropType.RIGHT_TOP), | ||
new Image(R.drawable.ball_horizontal, false, CropType.RIGHT_BOTTOM), | ||
new Image(R.drawable.ball_horizontal, false, CropType.RIGHT_CENTER), | ||
new Image(R.drawable.ball_vertical, false, CropType.CENTER_TOP), | ||
new Image(R.drawable.ball_vertical, false, CropType.CENTER_BOTTOM), | ||
new Image(R.drawable.ball_horizontal, false, CropType.LEFT_TOP), | ||
new Image(R.drawable.ball_horizontal, false, CropType.LEFT_CENTER), | ||
new Image(R.drawable.ball_horizontal, false, CropType.LEFT_BOTTOM), | ||
new Image(R.drawable.ball_horizontal, false, CropType.RIGHT_TOP), | ||
new Image(R.drawable.ball_horizontal, false, CropType.RIGHT_BOTTOM), | ||
new Image(R.drawable.ball_horizontal, false, CropType.RIGHT_CENTER) | ||
); | ||
} | ||
|
||
static List<Image> imagesWithRoundedCorners() { | ||
return Arrays.asList( | ||
// new Image(R.drawable.ball_vertical, false, CropType.NONE), | ||
// new Image(R.drawable.ball_horizontal, false, CropType.NONE), | ||
new Image(R.drawable.ball_vertical, true, CropType.CENTER_TOP), | ||
new Image(R.drawable.ball_vertical, true, CropType.CENTER_BOTTOM), | ||
new Image(R.drawable.ball_horizontal, true, CropType.LEFT_TOP), | ||
new Image(R.drawable.ball_horizontal, true, CropType.LEFT_CENTER), | ||
new Image(R.drawable.ball_horizontal, true, CropType.LEFT_BOTTOM), | ||
new Image(R.drawable.ball_horizontal, true, CropType.RIGHT_TOP), | ||
new Image(R.drawable.ball_horizontal, true, CropType.RIGHT_BOTTOM), | ||
new Image(R.drawable.ball_horizontal, true, CropType.RIGHT_CENTER), | ||
new Image(R.drawable.ball_vertical, false, CropType.CENTER_TOP), | ||
new Image(R.drawable.ball_vertical, false, CropType.CENTER_BOTTOM), | ||
new Image(R.drawable.ball_horizontal, false, CropType.LEFT_TOP), | ||
new Image(R.drawable.ball_horizontal, false, CropType.LEFT_CENTER), | ||
new Image(R.drawable.ball_horizontal, false, CropType.LEFT_BOTTOM), | ||
new Image(R.drawable.ball_horizontal, false, CropType.RIGHT_TOP), | ||
new Image(R.drawable.ball_horizontal, false, CropType.RIGHT_BOTTOM), | ||
new Image(R.drawable.ball_horizontal, false, CropType.RIGHT_CENTER), | ||
new Image(R.drawable.ball_vertical, false, CropType.CENTER_TOP), | ||
new Image(R.drawable.ball_vertical, false, CropType.CENTER_BOTTOM), | ||
new Image(R.drawable.ball_horizontal, false, CropType.LEFT_TOP), | ||
new Image(R.drawable.ball_horizontal, false, CropType.LEFT_CENTER), | ||
new Image(R.drawable.ball_horizontal, false, CropType.LEFT_BOTTOM), | ||
new Image(R.drawable.ball_horizontal, false, CropType.RIGHT_TOP), | ||
new Image(R.drawable.ball_horizontal, false, CropType.RIGHT_BOTTOM), | ||
new Image(R.drawable.ball_horizontal, false, CropType.RIGHT_CENTER), | ||
new Image(R.drawable.ball_vertical, false, CropType.CENTER_TOP), | ||
new Image(R.drawable.ball_vertical, false, CropType.CENTER_BOTTOM), | ||
new Image(R.drawable.ball_horizontal, false, CropType.LEFT_TOP), | ||
new Image(R.drawable.ball_horizontal, false, CropType.LEFT_CENTER), | ||
new Image(R.drawable.ball_horizontal, false, CropType.LEFT_BOTTOM), | ||
new Image(R.drawable.ball_horizontal, false, CropType.RIGHT_TOP), | ||
new Image(R.drawable.ball_horizontal, false, CropType.RIGHT_BOTTOM), | ||
new Image(R.drawable.ball_horizontal, false, CropType.RIGHT_CENTER) | ||
); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
...amples/src/main/java/com/cesards/samples/cropimageview/rounded_corners/ImagesAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package com.cesards.samples.cropimageview.rounded_corners; | ||
|
||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
|
||
import com.cesards.cropimageview.CropImageView; | ||
import com.cesards.samples.cropimageview.R; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.core.content.ContextCompat; | ||
import androidx.recyclerview.widget.RecyclerView; | ||
|
||
final class ImagesAdapter extends RecyclerView.Adapter<ImagesAdapter.ItemImageViewHolder> { | ||
|
||
private final List<Image> images = new ArrayList<>(); | ||
|
||
void removeAll() { | ||
if (images.isEmpty()) { | ||
return; | ||
} | ||
int imagesLength = images.size(); | ||
images.clear(); | ||
notifyItemRangeRemoved(0, imagesLength); | ||
} | ||
|
||
void add(Image image) { | ||
images.add(image); | ||
notifyItemInserted(this.images.size() - 1); | ||
} | ||
|
||
void add(List<Image> images) { | ||
this.images.addAll(images); | ||
notifyItemRangeInserted(this.images.size() - 1, images.size()); | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public ItemImageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { | ||
return new ItemImageViewHolder(LayoutInflater.from( | ||
parent.getContext()).inflate(R.layout.item_image, parent, false) | ||
); | ||
} | ||
|
||
@Override | ||
public void onBindViewHolder(@NonNull ItemImageViewHolder itemImageViewHolder, int position) { | ||
itemImageViewHolder.bind(images.get(position)); | ||
} | ||
|
||
@Override | ||
public int getItemCount() { | ||
return images.size(); | ||
} | ||
|
||
static class ItemImageViewHolder extends RecyclerView.ViewHolder { | ||
|
||
private final CropImageView cropImageView; | ||
|
||
ItemImageViewHolder(@NonNull View itemView) { | ||
super(itemView); | ||
this.cropImageView = (CropImageView) itemView; | ||
} | ||
|
||
void bind(Image image) { | ||
cropImageView.setImageDrawable(ContextCompat.getDrawable(itemView.getContext(), image.drawableResource())); | ||
cropImageView.croppedImage().withCropType(image.cropType()); | ||
} | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
...n/java/com/cesards/samples/cropimageview/rounded_corners/RoundedCornerImagesActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package com.cesards.samples.cropimageview.rounded_corners; | ||
|
||
import android.os.Bundle; | ||
import android.app.Activity; | ||
import android.view.View; | ||
import android.widget.AdapterView; | ||
import android.widget.ArrayAdapter; | ||
import android.widget.Spinner; | ||
|
||
import com.cesards.samples.cropimageview.R; | ||
import com.cesards.samples.cropimageview.rounded_corners.widget.VerticalTransparentItemDecorator; | ||
|
||
import androidx.appcompat.app.ActionBar; | ||
import androidx.appcompat.app.AppCompatActivity; | ||
import androidx.core.content.ContextCompat; | ||
import androidx.recyclerview.widget.DividerItemDecoration; | ||
import androidx.recyclerview.widget.RecyclerView; | ||
|
||
public class RoundedCornerImagesActivity extends AppCompatActivity { | ||
|
||
private ImagesAdapter imagesAdapter = new ImagesAdapter(); | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_rounded_corner_images); | ||
setupViews(savedInstanceState); | ||
} | ||
|
||
private void setupViews(Bundle savedInstanceState) { | ||
setSupportActionBar(findViewById(R.id.title_container)); | ||
// getSupportActionBar().setHomeButtonEnabled(true); | ||
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP); | ||
|
||
RecyclerView images = findViewById(R.id.images); | ||
images.addItemDecoration(new VerticalTransparentItemDecorator(getResources().getDimensionPixelOffset(R.dimen.activity_horizontal_margin))); | ||
images.setAdapter(imagesAdapter); | ||
|
||
Spinner navSpinner = findViewById(R.id.image_options); | ||
|
||
navSpinner.setAdapter( | ||
ArrayAdapter.createFromResource( | ||
navSpinner.getContext(), | ||
R.array.options, | ||
android.R.layout.simple_spinner_dropdown_item | ||
) | ||
); | ||
|
||
navSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { | ||
@Override | ||
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { | ||
switch (position) { | ||
case 0: | ||
imagesAdapter.removeAll(); | ||
imagesAdapter.add(ImageFactory.imagesWithoutRoundedCorners()); | ||
break; | ||
|
||
case 1: | ||
imagesAdapter.removeAll(); | ||
imagesAdapter.add(ImageFactory.imagesWithRoundedCorners()); | ||
break; | ||
|
||
default: | ||
throw new IllegalStateException("If it exists you can handle it."); | ||
} | ||
} | ||
|
||
@Override | ||
public void onNothingSelected(AdapterView<?> parent) { } | ||
}); | ||
|
||
if (savedInstanceState == null) { | ||
navSpinner.setSelection(0); | ||
} | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
...esards/samples/cropimageview/rounded_corners/widget/VerticalTransparentItemDecorator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.cesards.samples.cropimageview.rounded_corners.widget; | ||
|
||
import android.graphics.Rect; | ||
import android.view.View; | ||
|
||
import androidx.recyclerview.widget.RecyclerView; | ||
|
||
public class VerticalTransparentItemDecorator extends RecyclerView.ItemDecoration { | ||
|
||
private int space; | ||
|
||
public VerticalTransparentItemDecorator(int value) { | ||
this.space = value; | ||
} | ||
|
||
@Override | ||
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { | ||
// Skip first item in the list. | ||
if (parent.getChildAdapterPosition(view) != 0) { | ||
outRect.set(0, space, 0, 0); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.