我在一个社交网络应用程序中工作,我的要求是从图库中获取图像并以马赛克风格显示。它类似于 GridView ,但这里不同图像的大小彼此不同。我附上了一张图片来显示演示屏幕。
请为此建议我。
最佳答案
感谢 Siddharth Lele 的想法,让我得出了这个解决方案。我正在粘贴结构化的代码。我在安排格式时遇到了一些问题,现在我认为其他人可以承受这种压力。请记住,代码仅适用于 4.0 及更高版本。
activity_main.xml
<com.jake.quiltviewsample.QuiltView
android:id="@+id/quilt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dip" >
</com.jake.quiltviewsample.QuiltView>
</FrameLayout>
MainActivity.java
public class MainActivity extends Activity {
public QuiltView quiltView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
quiltView = (QuiltView) findViewById(R.id.quilt);
quiltView.setChildPadding(5);
addTestQuilts(200);
}
public void addTestQuilts(int num){
ArrayList<ImageView> images = new ArrayList<ImageView>();
for(int i = 0; i < num; i++){
ImageView image = new ImageView(this.getApplicationContext());
image.setScaleType(ScaleType.CENTER_CROP);
if(i % 2 == 0)
image.setImageResource(R.drawable.mayer);
else
image.setImageResource(R.drawable.mayer1);
images.add(image);
}
quiltView.addPatchImages(images);
}
}
QuiltView.java
public class QuiltView extends FrameLayout implements OnGlobalLayoutListener {
public QuiltViewBase quilt;
public ViewGroup scroll;
public int padding = 5;
public boolean isVertical = false;
public ArrayList<View> views;
private Adapter adapter;
public QuiltView(Context context,boolean isVertical) {
super(context);
this.isVertical = isVertical;
setup();
}
public QuiltView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.QuiltView);
String orientation = a.getString(R.styleable.QuiltView_scrollOrientation);
if(orientation != null){
if(orientation.equals("vertical")){
isVertical = true;
} else {
isVertical = false;
}
}
setup();
}
public void setup(){
views = new ArrayList<View>();
if(isVertical){
scroll = new ScrollView(this.getContext());
} else {
scroll = new HorizontalScrollView(this.getContext());
}
quilt = new QuiltViewBase(getContext(), isVertical);
scroll.addView(quilt);
this.addView(scroll);
}
private DataSetObserver adapterObserver = new DataSetObserver(){
public void onChanged(){
super.onChanged();
onDataChanged();
}
public void onInvalidated(){
super.onInvalidated();
onDataChanged();
}
public void onDataChanged(){
setViewsFromAdapter(adapter);
}
};
public void setAdapter(Adapter adapter){
this.adapter = adapter;
adapter.registerDataSetObserver(adapterObserver);
setViewsFromAdapter(adapter);
}
private void setViewsFromAdapter(Adapter adapter) {
this.removeAllViews();
for(int i = 0; i < adapter.getCount(); i++){
quilt.addPatch(adapter.getView(i, null, quilt));
}
}
public void addPatchImages(ArrayList<ImageView> images){
for(ImageView image: images){
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
image.setLayoutParams(params);
LinearLayout wrapper = new LinearLayout(this.getContext());
wrapper.setPadding(padding, padding, padding, padding);
wrapper.addView(image);
quilt.addPatch(wrapper);
}
}
public void addPatchViews(ArrayList<View> views_a){
for(View view: views_a){
quilt.addPatch(view);
}
}
public void addPatchesOnLayout(){
for(View view: views){
quilt.addPatch(view);
}
}
public void removeQuilt(View view){
quilt.removeView(view);
}
public void setChildPadding(int padding){
this.padding = padding;
}
public void refresh(){
quilt.refresh();
}
public void setOrientation(boolean isVertical){
this.isVertical = isVertical;
}
@Override
public void onGlobalLayout() {
//addPatchesOnLayout();
}
}
**QuiltViewBase.java**
public class QuiltViewBase extends GridLayout {
public int[] size;
public int columns;
public int rows;
public int view_width = -1;
public int view_height = -1;
public boolean isVertical = true;
public ArrayList<View> views;
public QuiltViewBase(Context context, boolean isVertical) {
super(context);
this.isVertical = isVertical;
if(view_width == -1){
DisplayMetrics metrics = this.getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels - 120;
view_width = width - this.getPaddingLeft() - this.getPaddingRight();
view_height = height - this.getPaddingTop() - this.getPaddingBottom();
}
views = new ArrayList<View>();
setup();
}
public void setup(){
if(isVertical){
setupVertical();
} else {
setupHorizontal();
}
}
public void setupVertical(){
size = getBaseSizeVertical();
this.setColumnCount(columns);
this.setRowCount(-1);
this.setOrientation(this.HORIZONTAL);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
this.setLayoutParams(params);
}
public void setupHorizontal(){
size = getBaseSizeHorizontal();
this.setRowCount(rows);
this.setColumnCount(-1);
this.setOrientation(this.VERTICAL);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
this.setLayoutParams(params);
}
public void addPatch(View view){
int count = this.getChildCount();
QuiltViewPatch child = QuiltViewPatch.init(count, columns);
GridLayout.LayoutParams params = new GridLayout.LayoutParams();
params.width = size[0]*child.width_ratio;
params.height = size[1]*child.height_ratio;
params.rowSpec = GridLayout.spec(Integer.MIN_VALUE, child.height_ratio);
params.columnSpec = GridLayout.spec(Integer.MIN_VALUE, child.width_ratio);
view.setLayoutParams(params);
addView(view);
views.add(view);
}
public void refresh(){
this.removeAllViewsInLayout();
setup();
for(View view : views){
addPatch(view);
}
}
public int[] getBaseSize(){
int[] size = new int[2];
float width_height_ratio = (3.0f/4.0f);
int base_width = getBaseWidth();
int base_height = (int) (base_width*width_height_ratio);
size[0] = base_width; // width
size[1] = base_height; // height
return size;
}
public int[] getBaseSizeVertical(){
int[] size = new int[2];
float width_height_ratio = (3.0f/4.0f);
int base_width = getBaseWidth();
int base_height = (int) (base_width*width_height_ratio);
size[0] = base_width; // width
size[1] = base_height; // height
return size;
}
public int[] getBaseSizeHorizontal(){
int[] size = new int[2];
float width_height_ratio = (4.0f/3.0f);
int base_height = getBaseHeight();
int base_width = (int) (base_height*width_height_ratio);
size[0] = base_width; // width
size[1] = base_height; // height
return size;
}
public int getBaseWidth(){
if(view_width < 500){
columns = 2;
} else if(view_width < 801){
columns = 3;
} else if(view_width < 1201){
columns = 4;
} else if(view_width < 1601){
columns = 5;
} else {
columns = 6;
}
return (view_width / columns);
}
public int getBaseHeight(){
if(view_height < 350){
rows = 2;
} else if(view_height < 650){
rows = 3;
} else if(view_height < 1050){
rows = 4;
} else if(view_height < 1250){
rows = 5;
} else {
rows = 6;
}
return (view_height / rows);
}
/*@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
view_width = parentWidth;
view_height = parentHeight;
setup(isVertical);
}*/
@Override
protected void onSizeChanged(int xNew, int yNew, int xOld, int yOld){
super.onSizeChanged(xNew, yNew, xOld, yOld);
view_width = xNew;
view_height = yNew;
}
}
**QuiltViewPatch.java**
public class QuiltViewPatch implements Comparable{
public int width_ratio;
public int height_ratio;
public QuiltViewPatch(int width_ratio, int height_ratio){
this.width_ratio = width_ratio;
this.height_ratio = height_ratio;
}
private static QuiltViewPatch create(Size size){
switch(size){
case Big:
return new QuiltViewPatch(2,2);
case Small:
return new QuiltViewPatch(1,1);
case Tall:
return new QuiltViewPatch(1,2);
}
return new QuiltViewPatch(1,1);
}
public int getHeightRatio(){return this.height_ratio;}
public int getWidthRatio(){return this.width_ratio;}
public static QuiltViewPatch create(int view_count){
if(view_count == 0)
return new QuiltViewPatch(2,2);
else if((view_count % 11) == 0)
return new QuiltViewPatch(2,2);
else if((view_count % 4) == 0)
return new QuiltViewPatch(1,2);
else
return new QuiltViewPatch(1,1);
}
private enum Size{
Big,
Small,
Tall
}
public static QuiltViewPatch init(int position, int column){
switch(column){
case 2:
return init2(position);
case 3:
return init3(position);
case 4:
return init4(position);
case 5:
return init5(position);
}
return init3(position);
}
private static QuiltViewPatch init2(int position){
switch(position % 15){
case 0:
return create(Size.Big);
case 1:
case 2:
case 3:
return create(Size.Small);
case 4:
return create(Size.Tall);
case 5:
case 6:
case 7:
return create(Size.Small);
case 8:
return create(Size.Tall);
case 9:
return create(Size.Tall);
case 10:
return create(Size.Small);
case 11:
return create(Size.Big);
case 12:
return create(Size.Tall);
case 13:
return create(Size.Tall);
case 14:
return create(Size.Small);
}
return create(Size.Small);
}
private static QuiltViewPatch init3(int position){
switch(position % 32){
case 0:
return create(Size.Big);
case 1:
case 2:
case 3:
return create(Size.Small);
case 4:
return create(Size.Tall);
case 5:
case 6:
case 7:
return create(Size.Small);
case 8:
return create(Size.Tall);
case 9:
case 10:
return create(Size.Small);
case 11:
return create(Size.Big);
case 12:
return create(Size.Tall);
case 13:
case 14:
return create(Size.Small);
case 15:
return create(Size.Small);
case 16:
return create(Size.Tall);
case 17:
case 18:
case 19:
return create(Size.Small);
case 20:
return create(Size.Tall);
case 21:
case 22:
return create(Size.Small);
case 23:
return create(Size.Big);
case 24:
return create(Size.Small);
case 25:
return create(Size.Tall);
case 26:
case 27:
case 28:
return create(Size.Small);
case 29:
return create(Size.Tall);
case 30:
case 31:
return create(Size.Small);
}
return create(Size.Small);
}
private static QuiltViewPatch init4(int position){
switch(position % 36){
case 0:
return create(Size.Big);
case 1:
case 2:
case 3:
return create(Size.Small);
case 4:
return create(Size.Tall);
case 5:
case 6:
case 7:
return create(Size.Small);
case 8:
return create(Size.Tall);
case 9:
case 10:
case 11:
return create(Size.Small);
case 12:
return create(Size.Big);
case 13:
return create(Size.Tall);
case 14:
case 15:
case 16:
return create(Size.Small);
case 17:
return create(Size.Tall);
case 18:
case 19:
case 20:
return create(Size.Small);
case 21:
return create(Size.Tall);
case 22:
case 23:
return create(Size.Small);
case 24:
return create(Size.Small);
case 25:
return create(Size.Big);
case 26:
return create(Size.Small);
case 27:
return create(Size.Tall);
case 28:
case 29:
case 30:
return create(Size.Small);
case 31:
return create(Size.Tall);
case 32:
case 33:
case 34:
case 35:
return create(Size.Small);
}
return create(Size.Small);
}
private static QuiltViewPatch init5(int position){
switch(position % 35){
case 0:
return create(Size.Big);
case 1:
case 2:
case 3:
return create(Size.Small);
case 4:
return create(Size.Tall);
case 5:
case 6:
case 7:
return create(Size.Small);
case 8:
return create(Size.Tall);
case 9:
case 10:
case 11:
return create(Size.Small);
case 12:
return create(Size.Big);
case 13:
return create(Size.Tall);
case 14:
case 15:
case 16:
return create(Size.Small);
case 17:
return create(Size.Tall);
case 18:
case 19:
case 20:
return create(Size.Small);
case 21:
return create(Size.Tall);
case 22:
case 23:
case 24:
return create(Size.Small);
case 25:
return create(Size.Big);
case 26:
return create(Size.Small);
case 27:
return create(Size.Tall);
case 28:
case 29:
case 30:
return create(Size.Small);
case 31:
return create(Size.Tall);
case 32:
return create(Size.Big);
case 33:
return create(Size.Tall);
case 34:
return create(Size.Small);
}
return create(Size.Small);
}
public static boolean getRandomBoolean(){
return (Math.random() < 0.5);
}
public boolean equals(Object obj){
if(obj != null && obj instanceof QuiltViewPatch){
QuiltViewPatch size = (QuiltViewPatch)obj;
return size.height_ratio == this.height_ratio && size.width_ratio == this.width_ratio;
}
return false;
}
public int hashCode(){
return height_ratio + 100 * width_ratio;
}
public String toString(){
return "Patch: " + height_ratio + " x " + width_ratio;
}
@Override
public int compareTo(Object another) {
if(another != null && another instanceof QuiltViewPatch){
QuiltViewPatch size = (QuiltViewPatch)another;
if(size.equals(this))
return 0;
if(this.height_ratio < size.height_ratio)
return -1;
else if(this.height_ratio > size.height_ratio)
return 1;
if(this.width_ratio < size.width_ratio)
return -1;
else
return 1;
}
return -1;
}
}
关于android - 在 Android 中以马赛克风格显示图库中的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17716325/
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时
作为我的Rails应用程序的一部分,我编写了一个小导入程序,它从我们的LDAP系统中吸取数据并将其塞入一个用户表中。不幸的是,与LDAP相关的代码在遍历我们的32K用户时泄漏了大量内存,我一直无法弄清楚如何解决这个问题。这个问题似乎在某种程度上与LDAP库有关,因为当我删除对LDAP内容的调用时,内存使用情况会很好地稳定下来。此外,不断增加的对象是Net::BER::BerIdentifiedString和Net::BER::BerIdentifiedArray,它们都是LDAP库的一部分。当我运行导入时,内存使用量最终达到超过1GB的峰值。如果问题存在,我需要找到一些方法来更正我的代
Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer
刚入门rails,开始慢慢理解。有人可以解释或给我一些关于在application_controller中编码的好处或时间和原因的想法吗?有哪些用例。您如何为Rails应用程序使用应用程序Controller?我不想在那里放太多代码,因为据我了解,每个请求都会调用此Controller。这是真的? 最佳答案 ApplicationController实际上是您应用程序中的每个其他Controller都将从中继承的类(尽管这不是强制性的)。我同意不要用太多代码弄乱它并保持干净整洁的态度,尽管在某些情况下ApplicationContr
我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢
我注意到像bundler这样的项目在每个specfile中执行requirespec_helper我还注意到rspec使用选项--require,它允许您在引导rspec时要求一个文件。您还可以将其添加到.rspec文件中,因此只要您运行不带参数的rspec就会添加它。使用上述方法有什么缺点可以解释为什么像bundler这样的项目选择在每个规范文件中都需要spec_helper吗? 最佳答案 我不在Bundler上工作,所以我不能直接谈论他们的做法。并非所有项目都checkin.rspec文件。原因是这个文件,通常按照当前的惯例,只
我正在使用active_admin,我在Rails3应用程序的应用程序中有一个目录管理,其中包含模型和页面的声明。时不时地我也有一个类,当那个类有一个常量时,就像这样:classFooBAR="bar"end然后,我在每个必须在我的Rails应用程序中重新加载一些代码的请求中收到此警告:/Users/pupeno/helloworld/app/admin/billing.rb:12:warning:alreadyinitializedconstantBAR知道发生了什么以及如何避免这些警告吗? 最佳答案 在纯Ruby中:classA